I'm having a hard time achieving what I need to achieve so I was wondering if someone here could help me :-)
I've seen the example 11.4. (List membership) on http://openbookproject.net/thinkcs/python/english3e/lists.html and it's quite close to my goal in some ways.
The project is:
Starting with a list of tuples refering to (key, [list of values])
my_list = [('a',[0]), ('b',[1]), ('c',[2]), ('a',[3])]I'd like to scan 'my_list' in order to append the nested lists, combining the lists of values for only one key, which would look like this :
my_list = [('a',[0, 3]), ('b',[1]), ('c',[2])]I succeeded to combine values manually but I'd like to automate it and I can't find how to do so! ^^
For now, here is what I've got :
# my_input == 'a b c a' #splitting input to list >>> raw_list = my_input.split() >>> raw_list ['a', 'b', 'c', 'a'] #getting an enumeration for each entry #### (in order of appearance, this is important!) #### >>> enum_list = [(b,[a]) for a, b in enumerate(raw_list)] >>> enum_list [('a', [0]), ('b', [1]), ('c', [2]), ('a', [3])] #trying to append the enum value of the second 'a' to the first tuple of 'a' >>> for (x, y) in enum_list : ... for (x, z) in enum_list : ... enum_list[enum_list.index((x, z))][1].append(y) ... >>> enum_list [('a', [0, [...], [1, [...], [...], [2, [...], [...], [...], [3, [...], [...], [...], [...]]], [3, [...], [...], [2, [...], [...], [...], [...]], [...]]], [2, [...], [1, [...], [...], [...], [3, [...], [...], [...], [...]]], [...], [3, [...], [1, [...], [...], [...], [...]], [...], [...]]], [3, [...], [1, [...], [...], [2, [...], [...], [...], [...]], [...]], [2, [...], [1, [...], [...], [...], [...]], [...], [...]], [...]]]), ('b', [1, [0, [...], [...], [2, [...], [...], [...], [3, [...], [...], [...], [...]]], [3, [...], [...], [2, [...], [...], [...], [...]], [...]]], [...], [2, [0, [...], [...], [...], [3, [...], [...], [...], [...]]], [...], [...], [3, [0, [...], [...], [...], [...]], [...], [...], [...]]], [3, [0, [...], [...], [2, [...], [...], [...], [...]], [...]], [...], [2, [0, [...], [...], [...], [...]], [...], [...], [...]], [...]]]), ('c', [2, [0, [...], [1, [...], [...], [...], [3, [...], [...], [...], [...]]], [...], [3, [...], [1, [...], [...], [...], [...]], [...], [...]]], [1, [0, [...], [...], [...], [3, [...], [...], [...], [...]]], [...], [...], [3, [0, [...], [...], [...], [...]], [...], [...], [...]]], [...], [3, [0, [...], [1, [...], [...], [...], [...]], [...], [...]], [1, [0, [...], [...], [...], [...]], [...], [...], [...]], [...], [...]]]), ('a', [3, [0, [...], [1, [...], [...], [2, [...], [...], [...], [...]], [...]], [2, [...], [1, [...], [...], [...], [...]], [...], [...]], [...]], [1, [0, [...], [...], [2, [...], [...], [...], [...]], [...]], [...], [2, [0, [...], [...], [...], [...]], [...], [...], [...]], [...]], [2, [0, [...], [1, [...], [...], [...], [...]], [...], [...]], [1, [0, [...], [...], [...], [...]], [...], [...], [...]], [...], [...]], [...]])]Sorry for the extra-long line, but I figured it would be more consistent with the whole error...
If I'm not clear enough, please don't hesitate to tell me and I'll give more details.
Thanks for your time and explanations :-)