python - find and update duplicates in a list of lists -
python - find and update duplicates in a list of lists -
i looking pythonic way solve next problem. have (what think is) working solution has complicated flow controls , isn't "pretty". (basically, c++ solution)
i have list of lists. each list contains multiple items of varying types (maybe 10 items per list) overall order of lists not relevant, order of items in individual list important. (ie can't alter it).
i looking "tag" duplicates adding field end of individual list. however, in case "duplicate" list 1 has equal values in several preselected fields, not fields (there no "true" duplicates).
for example: if original info 5 item list of lists , duplicate defined having equal values in first , 3rd fields:
['apple', 'window', 'pear', 2, 1.55, 'banana'] ['apple', 'orange', 'kiwi', 3, 1.80, 'banana'] ['apple', 'envelope', 'star_fruit', 2, 1.55, 'banana'] ['apple', 'orange', 'pear', 2, 0.80, 'coffee_cup'] ['apple', 'orange', 'pear', 2, 3.80, 'coffee_cup'] the first, 4th , 5th lists duplicates , hence lists should updated follows:
['apple', 'window', 'pear', 2, 1.55, 'banana', 1] ['apple', 'orange', 'kiwi', 3, 1.55, 'banana', 0] ['apple', 'envelope', 'star_fruit', 2, 1.55,'banana', 0] ['apple', 'orange', 'pear', 2, 3.80, 'coffee_cup', 2] ['apple', 'orange', 'pear', 2, 3.80, 'coffee_cup', 3] thanks help or direction. think may getting beyond learning python book.
from collections import defaultdict lists = [['apple', 'window', 'pear', 2, 1.55, 'banana'], ['apple', 'orange', 'kiwi', 3, 1.80, 'banana'], ['apple', 'envelope', 'star_fruit', 2, 1.55, 'banana'], ['apple', 'orange', 'pear', 2, 0.80, 'coffee_cup'], ['apple', 'orange', 'pear', 2, 3.80, 'coffee_cup']] dic = defaultdict(int) fts = [] lst in lists: first_third = lst[0], lst[2] dic[first_third] += 1 if dic[first_third] == 2: fts.append(first_third) lst.append(dic[first_third]) lst in lists: if (lst[0], lst[2]) not in fts: lst[-1] -= 1 print(lists)
edit: utdemir. first_third = lst[0], lst[2] correct, not first_third = lst[0] + lst[2]
edit2: changed variable names clarity.
edit3: changed reflect original poster wanted, , updated list. not pretty more, desired changes tacked on.
python list duplicates
Comments
Post a Comment