Problem with List of List

K

Kirt

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
 
F

Fredrik Lundh

Kirt said:
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.html#SECTION006200000000000000000

and explained in further detail in the language reference:

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

</F>
 
F

Fredrik Lundh

Fredrik said:
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>
 
K

Kirt

Fredrik said:
Fredrik said:
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.
 
K

Kirt

Fredrik said:
Fredrik said:
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.
 
K

Kirt

Kirt said:
Fredrik said:
Fredrik said:
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..........
 
P

Paul Rubin

Kirt said:
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:
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?
 
F

Fredrik Lundh

Kirt said:
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>
 
K

Kirt

Paul said:
Kirt said:
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:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top