Tuesday, 15 September 2015

python - comparing nested lists and editing -



python - comparing nested lists and editing -

sorry if sounds silly question problem has gotten me confused. i'm new python, maybe i'm missing something. did research haven't gotten far. here goes:

i'm going utilize simple illustration makes question clearer, info different format , required action same. have database of people , pizzas eat (and other data). our database has multiple entries of same people different pizzas (because combined info gotten different pizzerias).

example dataset:

alldata = [['joe','32', 'pepperoni,cheese'],['marc','24','cheese'],['jill','27','veggie supreme, cheese'],['joe','32','pepperoni,veggie supreme']['marc','25','cheese,chicken supreme']]

few things notice , rules want follow:

names can appear multiple times though in specific case know entries same name same person.

the age can different same person in different entries, pick first age encountered of person , utilize it. illustration marc's age 24 , ignore 25 sec entry

i want edit info person's name appears once, , pizzas eats unique set entries same name. mentioned before age first 1 encountered. therefore, i'd want final info this:

fixeddata = [['joe','32','pepperoni,cheese,veggie supreme'],['marc','24','cheese,chicken supreme'],['jill','27','veggie supreme, cheese']]

i'm thinking on lines of:

fixeddata = [] in alldata: if i[0] not in fixeddata[0]: fixeddata.append[i] else: fixeddata[i[-1]]=set(fixeddata[i[-1]],i[-1])

i know i'm making several mistakes. please please point me towards right direction?

thanks heaps.

since names unique, makes sense utilize them keys in dict, name key. much more appropriate in case:

>>> d = {} >>> in alldata: if i[0] in d: d[i[0]][-1] = list(set(d[i[0]][-1] + (i[-1].split(',')))) else: d[i[0]] = [i[1],i[2].split(',')] >>> d {'jill': ['27', ['veggie supreme', ' cheese']], 'joe': ['32', ['pepperoni', 'cheese', 'pepperoni', 'veggie supreme']], 'marc': ['24', ['cheese', 'cheese', 'chicken supreme']]}

python nested-lists

No comments:

Post a Comment