Python: Nested List Modification -


i have nested list of paired data in format:

mylist = [['item1', 'some other stuff', 'value1'],['item1', 'some other stuff', 'value2'],['item2', 'some other stuff', 'value3'],['item2', 'some other stuff', 'value4']] 

i have no idea how following, need to:

i need list grouped such:

[['item1', 'value1', 'value2'], ['item2', 'value3', 'value4']] 

so list of items, of values should grouped corresponding item if item repeated multiple times in list different values.

any appreciated.

thanks

let's start out using dictionary, map items lists of values. that's going lot easier (and faster) list, because figure out list add new value mydict[item] instead of having write kind of linear-search function.

mydict = {} item, otherstuff, value in mylist:     mydict.setdefault(item, []).append(value) 

this gives you:

{'item1': ['value1', 'value2'], 'item2': ['value3', 'value4']} 

now can convert dictionary list, if want:

groupedlist = [[k] + v k, v in mydict.items()] 

this gives you:

[['item2', 'value3', 'value4'], ['item1', 'value1', 'value2']] 

the big downside here once stick things dict, lose original order. if expecting item1 come first because first entry came before item2's first entry (or because item2's last entry came after item1's maybe?), you've lost that. if it's important, can use ordereddict.

the big upside often, want dictionary in end, not list.

the smaller upside that, if data aren't sorted, groupby(…sorted(…)) requires o(nlogn) sort, while solution o(n). usually, won't make difference. , if does, constant-factor differences given python implementation , platform might outweigh differences anyway. if performance matters, test both solutions , use faster one.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -