python - How to make an index combining values of lists nested in tuples? -


i'm having hard time achieving need achieve wondering if here me :-)

i've seen example 11.4. (list membership) on http://openbookproject.net/thinkcs/python/english3e/lists.html , it's quite close goal in ways.

the project is:

  • starting list of tuples refering (key, [list of values])

    my_list = [('a',[0]), ('b',[1]), ('c',[2]), ('a',[3])] 
  • i'd scan 'my_list' in order append nested lists, combining lists of values 1 key, :

    my_list = [('a',[0, 3]), ('b',[1]), ('c',[2])] 
  • i succeeded combine values manually i'd automate , can't find how so! ^^

  • for now, here i've got :

    # my_input == 'a b c a'  #splitting input list >>> raw_list = my_input.split() >>> raw_list ['a', 'b', 'c', 'a']  #getting enumeration each entry #### (in order of appearance, important!) #### >>> enum_list = [(b,[a]) a, b in enumerate(raw_list)] >>> enum_list [('a', [0]), ('b', [1]), ('c', [2]), ('a', [3])]  #trying append enum value of second 'a' first tuple of 'a' >>> (x, y) in enum_list : ...     (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 extra-long line, figured more consistent whole error...

if i'm not clear enough, please don't hesitate tell me , i'll give more details.

thanks time , explanations :-)

you can use dictionary construct list

my_list = [('a',[0]), ('b',[1]), ('c',[2]), ('a',[3])] my_dict = {}  item in my_list:     if item[0] in my_dict.keys():         ### if key exists append list         my_dict[item[0]].extend(item[1][0:])     else:         ### if key not exist create new key-value pair         my_dict[item[0]] = [item[1][0]]  my_list = my_dict.items() print my_list 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -