Problem with List of List

Discussion in 'Python' started by Kirt, Aug 26, 2006.

  1. Kirt

    Kirt Guest

    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
     
    Kirt, Aug 26, 2006
    #1
    1. Advertising

  2. 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.html#SECTION006200000000000000000

    and explained in further detail in the language reference:

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

    </F>
     
    Fredrik Lundh, Aug 26, 2006
    #2
    1. Advertising

  3. 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>
     
    Fredrik Lundh, Aug 26, 2006
    #3
  4. Kirt

    Kirt Guest

    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.
     
    Kirt, Aug 26, 2006
    #4
  5. Kirt

    Kirt Guest

    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.
     
    Kirt, Aug 26, 2006
    #5
  6. Kirt

    Kirt Guest

    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..........
     
    Kirt, Aug 26, 2006
    #6
  7. Kirt

    Paul Rubin Guest

    "Kirt" <> 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?
     
    Paul Rubin, Aug 26, 2006
    #7
  8. 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>
     
    Fredrik Lundh, Aug 26, 2006
    #8
  9. Kirt

    Kirt Guest

    Paul Rubin wrote:
    > "Kirt" <> 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.
     
    Kirt, Aug 26, 2006
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. roopa
    Replies:
    6
    Views:
    751
    Jerry Coffin
    Aug 27, 2004
  2. dackz
    Replies:
    0
    Views:
    489
    dackz
    Feb 6, 2007
  3. Debajit Adhikary
    Replies:
    17
    Views:
    691
    Debajit Adhikary
    Oct 18, 2007
  4. OW Ghim Siong
    Replies:
    2
    Views:
    394
    Peter Otten
    Nov 30, 2010
  5. asil klin
    Replies:
    28
    Views:
    1,300
    Andreas Leitgeb
    Mar 5, 2011
Loading...

Share This Page