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
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.Originally Posted by harry3490
If so, you might do:
and print “d”.Code:d = {} for sublist in list1: car, *cdr = sublist if car in d: d[car].extend(cdr) else: d[car] = cdr[:]
My armada: Debian GNU/Linux 8 (desktop, home laptop, work laptop), Raspbian GNU/Linux 8 (nameserver), Ubuntu 14.04.3 LTS (HTPC), PC-BSD 10.2 (testbed), Android 4.2.1 (tablet)
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}..
In which case, you should have them surrounded by quotes.Originally Posted by harry3490
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: Debian GNU/Linux 8 (desktop, home laptop, work laptop), Raspbian GNU/Linux 8 (nameserver), Ubuntu 14.04.3 LTS (HTPC), PC-BSD 10.2 (testbed), Android 4.2.1 (tablet)
Yep my first thought was parse list1 into a dictionary of lists, too.
yeah it is working !! thanks oscar![]()