python Combining items in a list of lists based on contents -


i have list of lists have different amount of lists each time depending on other conditions. each of list contains 4 items. want if elements 1,2,3 of 2 of inner lists same, add element 0 , delete duplicates. list looks this:

 [     [4, 'blue', 'round', none],     [6, 'blue', 'round', none],     [8, 'red', 'round', none],     [10, 'red', 'round', none],     [8, 'red', 'square', none], ] 

i think making new list might not sure. need end product be:

[     [10, 'blue', 'round', none],     [18, 'red', 'round', none],     [8, 'red', 'square', none], ] 

the amount of different lists inside list different. appreciated.

roughly speaking, doing associating element 0 tuple defined last 3 elements of given list. luckily, it's okay tuple key of dictionary!

from collections import ordereddict color_data = [[4,'blue','round','none'], [6,'blue','round','none'], [8,'red','round','none'], [10,'red','round','none'], [8,'red','square','none']] data = ordereddict() x in color_data:     key = tuple(x[1:])     value = data.setdefault(key, 0) + x[0]     data[key] = value color_data = [ [ value] + list(key) key, value in data.items() ] 

Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -