Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Problem with List of List

Reply
Thread Tools

Problem with List of List

 
 
Kirt
Guest
Posts: n/a
 
      08-26-2006
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

 
Reply With Quote
 
 
 
 
Fredrik Lundh
Guest
Posts: n/a
 
      08-26-2006
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>

 
Reply With Quote
 
 
 
 
Fredrik Lundh
Guest
Posts: n/a
 
      08-26-2006
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>

 
Reply With Quote
 
Kirt
Guest
Posts: n/a
 
      08-26-2006
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.

 
Reply With Quote
 
Kirt
Guest
Posts: n/a
 
      08-26-2006
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.

 
Reply With Quote
 
Kirt
Guest
Posts: n/a
 
      08-26-2006
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..........

 
Reply With Quote
 
Paul Rubin
Guest
Posts: n/a
 
      08-26-2006
"Kirt" <(E-Mail Removed)> writes:
> 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?
 
Reply With Quote
 
Fredrik Lundh
Guest
Posts: n/a
 
      08-26-2006
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>

 
Reply With Quote
 
Kirt
Guest
Posts: n/a
 
      08-26-2006

Paul Rubin wrote:
> "Kirt" <(E-Mail Removed)> writes:
> > 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java Collections List : Converting from List '<Column <String1,String2>>' to 'List <String1>' asil klin Java 28 03-05-2011 01:59 AM
Memory issues when storing as List of Strings vs List of List OW Ghim Siong Python 2 11-30-2010 12:22 PM
Appending a list's elements to another list using a list comprehension Debajit Adhikary Python 17 10-18-2007 06:45 PM
Why does list.__getitem__ return a list instance for subclasses ofthe list type? dackz Python 0 02-06-2007 04:44 PM
Difference Between List x; and List x(); , if 'List' is a Class? roopa C++ 6 08-27-2004 06:18 PM



Advertisments