python - Conversion from dictionary to list preserving elements -


i've problem conversion this:

counter({('pintor', 'ncms000'): 1, ('ser', 'vsis3s0'): 1, ('muralista', 'aq0cs0'): 1, ('diego_rivera', 'np00000'): 1, ('frida_kahlo', 'np00000'): 1, ('caso', 'ncms000'): 1}) 

that obtained code:

res = collections.counter(map(tuple, listapalabras)) return res 

but need list in form:

[['pintor', 'ncms000', 1], ['ser', 'vsis3s0', 1], ['muralista', 'aq0cs0', 1], ['diego_rivera', 'np00000', 1], ['frida_kahlo', 'np00000', 1], ('caso', 'ncms000', 1]] 

loop on counter list comprehension:

[list(k) + [v] k, v in res.iteritems()] 

demo:

>>> collections import counter >>> res = counter({('pintor', 'ncms000'): 1, ('ser', 'vsis3s0'): 1, ('muralista', 'aq0cs0'): 1, ('diego_rivera', 'np00000'): 1, ('frida_kahlo', 'np00000'): 1, ('caso', 'ncms000'): 1}) >>> [list(k) + [v] k, v in res.iteritems()] [['pintor', 'ncms000', 1], ['ser', 'vsis3s0', 1], ['diego_rivera', 'np00000', 1], ['frida_kahlo', 'np00000', 1], ['muralista', 'aq0cs0', 1], ['caso', 'ncms000', 1]] 

if wanted values listed in sorted order, use .most_common() instead of .iteritems().


Comments

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -