Problem with List of List

Kirt moqtar at gmail.com
Sat Aug 26 02:34:03 EDT 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..........




More information about the Python-list mailing list