473,394 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Problem with List of List

I am a newbie and stuck over this for a week
I have a code
==================CODE=============
List=[['1', 'a', '6'], ['1', 'b', '6'], ['1', 'c', '6'],
['1', 'd', '6'],['2', 'a','6'], ['2', 'b', '6'],
['2', 'c', '6'], ['2', 'd', '6'], ['3', 'a', '6'],
['3','b', '6'], ['4', 'a', '6'], ['4', 'b', '6']]
for x in List:
temp=[]
print x
for y in List:
if x[0]==y[0]:
print y[0],y[1]
temp.append(y)
for z in temp:
List.remove(z)
print 'rem', z
=======================================
the output i am getting at Present is:
==========Current OP=====================
['1', 'a', '6']
1 a
1 b
1 c
1 d
rem ['1', 'a', '6']
rem ['1', 'b', '6']
rem ['1', 'c', '6']
rem ['1', 'd', '6']

['2', 'b', '6']
2 a
2 b
2 c
2 d
rem ['2', 'a', '6']
rem ['2', 'b', '6']
rem ['2', 'c', '6']
rem ['2', 'd', '6']

['4', 'a', '6']
4 a
4 b
rem ['4', 'a', '6']
rem ['4', 'b', '6']
================================================
i am wondering where " ['3', 'a', '6'], ['3','b', '6']" is not showing.

I want an output like this:

['1', 'a', '6']
1 a
1 b
1 c
1 d
rem ['1', 'a', '6']
rem ['1', 'b', '6']
rem ['1', 'c', '6']
rem ['1', 'd', '6']

['2', 'b', '6']
2 a
2 b
2 c
2 d
rem ['2', 'a', '6']
rem ['2', 'b', '6']
rem ['2', 'c', '6']
rem ['2', 'd', '6']

['3', 'a', '6'] ================
3 a why is this portion
3 b not present in my current op
rem ['3', 'a', '6']
rem ['3', 'b', '6'] =================

['4', 'a', '6']
4 a
4 b
rem ['4', 'a', '6']
rem ['4', 'b', '6']

I hope ull wil get what i am trying to say. Can anyone help: Thanx

Aug 26 '06 #1
8 1326
Kirt wrote:
I am a newbie and stuck over this for a week
that's unfortunate.
I have a code
==================CODE=============
List=[['1', 'a', '6'], ['1', 'b', '6'], ['1', 'c', '6'],
['1', 'd', '6'],['2', 'a','6'], ['2', 'b', '6'],
['2', 'c', '6'], ['2', 'd', '6'], ['3', 'a', '6'],
['3','b', '6'], ['4', 'a', '6'], ['4', 'b', '6']]
for x in List:
temp=[]
print x
for y in List:
if x[0]==y[0]:
print y[0],y[1]
temp.append(y)
for z in temp:
List.remove(z)
print 'rem', z
the for loop uses an internal index to fetch items from the list you're
looping over, so if you remove items from it, you'll end up skipping
over items. this is hinted at in the tutorial:

http://docs.python.org/tut/node6.htm...00000000000000

and explained in further detail in the language reference:

http://docs.python.org/ref/for.html
http://pyref.infogami.com/for

</F>

Aug 26 '06 #2
Fredrik Lundh wrote:
>I have a code
==================CODE=============
List=[['1', 'a', '6'], ['1', 'b', '6'], ['1', 'c', '6'],
['1', 'd', '6'],['2', 'a','6'], ['2', 'b', '6'],
['2', 'c', '6'], ['2', 'd', '6'], ['3', 'a', '6'],
['3','b', '6'], ['4', 'a', '6'], ['4', 'b', '6']]
for x in List:
temp=[]
print x
for y in List:
if x[0]==y[0]:
print y[0],y[1]
temp.append(y)
for z in temp:
List.remove(z)
print 'rem', z

the for loop uses an internal index to fetch items from the list you're
looping over, so if you remove items from it, you'll end up skipping
over items.
forgot to mention that the fix is to change the first for statement to:

for x in List[:]:

</F>

Aug 26 '06 #3
Fredrik Lundh wrote:
Fredrik Lundh wrote:
I have a code
==================CODE=============
List=[['1', 'a', '6'], ['1', 'b', '6'], ['1', 'c', '6'],
['1', 'd', '6'],['2', 'a','6'], ['2', 'b', '6'],
['2', 'c', '6'], ['2', 'd', '6'], ['3', 'a', '6'],
['3','b', '6'], ['4', 'a', '6'], ['4', 'b', '6']]
for x in List:
temp=[]
print x
for y in List:
if x[0]==y[0]:
print y[0],y[1]
temp.append(y)
for z in temp:
List.remove(z)
print 'rem', z
the for loop uses an internal index to fetch items from the list you're
looping over, so if you remove items from it, you'll end up skipping
over items.

forgot to mention that the fix is to change the first for statement to:

for x in List[:]:

</F>
Thanx Fredrik Lundh for ur response. I tried ur fix But the output i am
getting is repeated.

for x in List[:]:
t2=[]
print x[0]
for y in List:
if x[0]==y[0]:
print y[1],y[2]
t2.append(y)

for z in t2:
List[:].remove(z)

The output i am getting is now is:

1
a 6
b 6
c 6
d 6

1
a 6
b 6
c 6
d 6

1
a 6
b 6
c 6
d 6
1
a 6
b 6
c 6
d 6

2
a 6
b 6
c 6
d 6

2
a 6
b 6
c 6
d 6

2
a 6
b 6
c 6
d 6

2
a 6
b 6
c 6
d 6

3
a 6
b 6

3
a 6
b 6

4
a 6
b 6

4
a 6
b 6

Can u show me where i am going wrong.

Aug 26 '06 #4
Fredrik Lundh wrote:
Fredrik Lundh wrote:
I have a code
==================CODE=============
List=[['1', 'a', '6'], ['1', 'b', '6'], ['1', 'c', '6'],
['1', 'd', '6'],['2', 'a','6'], ['2', 'b', '6'],
['2', 'c', '6'], ['2', 'd', '6'], ['3', 'a', '6'],
['3','b', '6'], ['4', 'a', '6'], ['4', 'b', '6']]
for x in List:
temp=[]
print x
for y in List:
if x[0]==y[0]:
print y[0],y[1]
temp.append(y)
for z in temp:
List.remove(z)
print 'rem', z
the for loop uses an internal index to fetch items from the list you're
looping over, so if you remove items from it, you'll end up skipping
over items.

forgot to mention that the fix is to change the first for statement to:

for x in List[:]:

</F>
Thanx Fredrik Lundh for ur response. I tried ur fix But the output i am
getting is repeated.

for x in List[:]:
t2=[]
print x[0]
for y in List:
if x[0]==y[0]:
print y[1],y[2]
t2.append(y)

for z in t2:
List[:].remove(z)

The output i am getting is now is:

1
a 6
b 6
c 6
d 6

1
a 6
b 6
c 6
d 6

1
a 6
b 6
c 6
d 6
1
a 6
b 6
c 6
d 6

2
a 6
b 6
c 6
d 6

2
a 6
b 6
c 6
d 6

2
a 6
b 6
c 6
d 6

2
a 6
b 6
c 6
d 6

3
a 6
b 6

3
a 6
b 6

4
a 6
b 6

4
a 6
b 6

Can u show me where i am going wrong.

Aug 26 '06 #5
Kirt wrote:
Fredrik Lundh wrote:
Fredrik Lundh wrote:
>I have a code
>==================CODE=============
>List=[['1', 'a', '6'], ['1', 'b', '6'], ['1', 'c', '6'],
> ['1', 'd', '6'],['2', 'a','6'], ['2', 'b', '6'],
> ['2', 'c', '6'], ['2', 'd', '6'], ['3', 'a', '6'],
> ['3','b', '6'], ['4', 'a', '6'], ['4', 'b', '6']]
>>
>>
>for x in List:
> temp=[]
> print x
> for y in List:
> if x[0]==y[0]:
> print y[0],y[1]
> temp.append(y)
> for z in temp:
> List.remove(z)
> print 'rem', z
>
the for loop uses an internal index to fetch items from the list you're
looping over, so if you remove items from it, you'll end up skipping
over items.
forgot to mention that the fix is to change the first for statement to:

for x in List[:]:

</F>
Thanx Fredrik Lundh for ur response. I tried ur fix But the output i am
getting is repeated.

for x in List[:]:
t2=[]
print x[0]
for y in List:
if x[0]==y[0]:
print y[1],y[2]
t2.append(y)

for z in t2:
List[:].remove(z)

The output i am getting is now is:

1
a 6
b 6
c 6
d 6

1
a 6
b 6
c 6
d 6

1
a 6
b 6
c 6
d 6
1
a 6
b 6
c 6
d 6

2
a 6
b 6
c 6
d 6

2
a 6
b 6
c 6
d 6

2
a 6
b 6
c 6
d 6

2
a 6
b 6
c 6
d 6

3
a 6
b 6

3
a 6
b 6

4
a 6
b 6

4
a 6
b 6

Can u show me where i am going wrong.
Actually here in my code ['1', 'a', '6'], ['1', 'b', '6'], ['1', 'c',
'6'] means:
1==Directory name;
a,b,c,d=== Filename
6 ==modified time

So i want the out put like:
Directory name: 1

Filename:a
modified time:6
Filename:b
modified time:6
Filename:c
modified time:6
and so on..........

Aug 26 '06 #6
"Kirt" <mo****@gmail.comwrites:
Actually here in my code ['1', 'a', '6'], ['1', 'b', '6'], ['1', 'c',
'6'] means:
1==Directory name;
a,b,c,d=== Filename
6 ==modified time

So i want the out put like:
Directory name: 1
Filename:a
modified time:6
Try writing the code in a style with fewer side effects. I like using
itertools.groupby for this type of thing (not exactly tested):

from itertools import groupby

List=[['1', 'a', '6'], ['1', 'b', '6'], ['1', 'c', '6'],
['1', 'd', '6'],['2', 'a','6'], ['2', 'b', '6'],
['2', 'c', '6'], ['2', 'd', '6'], ['3', 'a', '6'],
['3','b', '6'], ['4', 'a', '6'], ['4', 'b', '6']]

# retrieve directory name from one of those tuples
def directory_name(t): return t[0]

for dirname,dir_contents in groupby(sorted(List), directory_name):
print 'Directory name:, dirname
for dname2,filename,modtime in dir_contents:
print 'Filename:%s\nmodified time:%s'% (filename, modtime)

The output is:
>>## working on region in file /usr/tmp/python-9013z7X...
Directory name: 1
Filename:a
modified time:6
Filename:b
modified time:6
Filename:c
modified time:6
Filename:d
modified time:6
Directory name: 2
Filename:a
modified time:6
Filename:b
modified time:6
Filename:c
modified time:6
Filename:d
modified time:6
Directory name: 3
Filename:a
modified time:6
Filename:b
modified time:6
Directory name: 4
Filename:a
modified time:6
Filename:b
modified time:6
>>>
Is that what you wanted?
Aug 26 '06 #7
Kirt wrote:
>forgot to mention that the fix is to change the first for statement to:

for x in List[:]:

</F>
Thanx Fredrik Lundh for ur response. I tried ur fix But the output i am
getting is repeated.
I'm pretty sure I told you to *loop* over a copy, not *remove* things
from a copy.
for x in List[:]:
t2=[]
print x[0]
for y in List:
if x[0]==y[0]:
print y[1],y[2]
t2.append(y)

for z in t2:
List[:].remove(z)
what's that last line supposed to to?

here's your *original* code, changed to loop over a copy:

for x in List[:]:
temp=[]
print x
for y in List:
if x[0]==y[0]:
print y[0],y[1]
temp.append(y)
for z in temp:
List.remove(z)
print 'rem', z

when run on your original data set, this prints your expected output.

</F>

Aug 26 '06 #8

Paul Rubin wrote:
"Kirt" <mo****@gmail.comwrites:
Actually here in my code ['1', 'a', '6'], ['1', 'b', '6'], ['1', 'c',
'6'] means:
1==Directory name;
a,b,c,d=== Filename
6 ==modified time

So i want the out put like:
Directory name: 1
Filename:a
modified time:6

Try writing the code in a style with fewer side effects. I like using
itertools.groupby for this type of thing (not exactly tested):

from itertools import groupby

List=[['1', 'a', '6'], ['1', 'b', '6'], ['1', 'c', '6'],
['1', 'd', '6'],['2', 'a','6'], ['2', 'b', '6'],
['2', 'c', '6'], ['2', 'd', '6'], ['3', 'a', '6'],
['3','b', '6'], ['4', 'a', '6'], ['4', 'b', '6']]

# retrieve directory name from one of those tuples
def directory_name(t): return t[0]

for dirname,dir_contents in groupby(sorted(List), directory_name):
print 'Directory name:, dirname
for dname2,filename,modtime in dir_contents:
print 'Filename:%s\nmodified time:%s'% (filename, modtime)

The output is:
>>## working on region in file /usr/tmp/python-9013z7X...
Directory name: 1
Filename:a
modified time:6
Filename:b
modified time:6
Filename:c
modified time:6
Filename:d
modified time:6
Directory name: 2
Filename:a
modified time:6
Filename:b
modified time:6
Filename:c
modified time:6
Filename:d
modified time:6
Directory name: 3
Filename:a
modified time:6
Filename:b
modified time:6
Directory name: 4
Filename:a
modified time:6
Filename:b
modified time:6
>>>

Is that what you wanted?
Thanx Paul. Thats exactly what i wanted. ur piece of code has reduced
my overall code by half. thanx again.

Aug 26 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Clifford W. Racz | last post by:
Has anyone solved the issue of translating lists in Word 2003 (WordML) into xHTML? I have been trying to get the nested table code for my XSLT to work for a while now, with no way to get the...
10
by: Michael Strorm | last post by:
Hi! I've been having problems with a DTD. Having had the Sun XML validator reject a document, I put it through 'xmllint' for more information. 'Xmllint' noted a problem with the DTD itself;...
2
by: Pascal Deparis | last post by:
Hi, I've got a problem with the DB2 Command Line Processor, in V8, FP1. The databases have been created on an AIX server (version 4.3.3). Have a look at this: >db2 ? list DB21034E The...
5
by: John N. | last post by:
Hi All, Here I have a linked list each containing a char and is double linked. Then I have a pointer to an item in that list which is the current insertion point. In this funtion, the user...
57
by: Xarky | last post by:
Hi, I am writing a linked list in the following way. struct list { struct list *next; char *mybuff; };
1
by: cody | last post by:
I have a OOP problem with the well known pattern where objects containing an object which represents a list of subobjects. Now my problem is that the ctor of a subobject indirectly calls the...
14
by: rurpy | last post by:
Another Python docs problem... I was trying to use imp.find_module(). >>> imp.find_module("mymod", "./subdir") ImportError: No frozen submodule named ./subdir.mymod subdir/mymod.py...
2
by: Mike Collins | last post by:
I cannot get the correct drop down list value from a drop down I have on my web form. I get the initial value that was loaded in the list. It was asked by someone else what the autopostback was...
4
by: FBM | last post by:
Hi, I am working on a program that simulates one of the elements of ATM. The simulation stores events which occurs every some milliseconds for a certain amount of time. Every time that an event...
7
by: Fernando Barsoba | last post by:
Hi, After following the advice received in this list, I have isolated the memory leak problem I am having. I am also using MEMWATCH and I think it is working properly. The program does some...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.