Thread: List problem

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0

    List problem


    if list1={[x,1,3,4],[y,5,6,7],[x,2,3,4],[z,1,2,3],[y,6,7,8],[hello,3,4,5],[hello,66,99,11]}

    I want to print like
    For x: values are 1,3,4,2,3,4
    For y: values are 5,6,7,6,7,8
    For z: values are 1,2,3
    For Hello: values are 3,4,5,66,99,11
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2007
    Location
    Joensuu, Finland
    Posts
    430
    Rep Power
    66
    Originally Posted by harry3490
    if list1={[x,1,3,4],[y,5,6,7],[x,2,3,4],[z,1,2,3],[y,6,7,8],[hello,3,4,5],[hello,66,99,11]}

    I want to print like
    For x: values are 1,3,4,2,3,4
    For y: values are 5,6,7,6,7,8
    For z: values are 1,2,3
    For Hello: values are 3,4,5,66,99,11
    You cannot mean quite this. Lists are unhashable, so you cannot create sets whose elements were lists. Also, you possibly meant x, y and hello to be strings, not variables.

    If so, you might do:
    Code:
    d = {}
    for sublist in list1:
        car, *cdr = sublist
        if car in d:
            d[car].extend(cdr)
        else:
            d[car] = cdr[:]
    and print d.
    My armada: openSUSE 13.1 (home desktop, home laptop), Crunchbang Linux 11 (mini laptop, work laptop), Android 4.2.1 (tablet)
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0
    First of all, here x,y,z,hello are string not a variable. And list is defined as like list1={tuple1,tuple2,.........}
    tuple1=(x,2,5,6}..
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2007
    Location
    Joensuu, Finland
    Posts
    430
    Rep Power
    66
    Originally Posted by harry3490
    First of all, here x,y,z,hello are string not a variable.
    In which case, you should have them surrounded by quotes.

    And list is defined as like list1={tuple1,tuple2,.........}
    tuple1=(x,2,5,6}..
    Again, you use {...} as if you were defining a set instead of a list. Tuples are hashable and can be used in a set, but in your original code you had [...] instead of (...), i.e., lists instead of tuples.

    But since both my guesses were correct, the code I provided should work, right?
    My armada: openSUSE 13.1 (home desktop, home laptop), Crunchbang Linux 11 (mini laptop, work laptop), Android 4.2.1 (tablet)
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Location
    Ranchos de Taos, NM
    Posts
    3
    Rep Power
    0
    Yep my first thought was parse list1 into a dictionary of lists, too.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0
    yeah it is working !! thanks oscar

IMN logo majestic logo threadwatch logo seochat tools logo