Python list of tuples deduplication -


i trying deduplicate set of different lists of tuples 1 after other. lists like:

a = [      (('x','y','z',2,3,4), ('a','b','c',5,10,11)),      (('a','b','c',5,10,11), ('x','y','z',2,3,4)),      (('t','f','j',0,1,0), ('h','g','k',2,8,7)),      ...                                          ]  b = [      (('x','y','z',0,0,0), ('a','b','c',3,3,2)),      (('a','b','c',3,3,2), ('x','y','z',0,0,0)),      (('j','k','l',5,4,3), ('v','t','d',5,10,12)),      ...                                          ] 

i running (e.g.for list a):

from collections import ordereddict values = [[x,y] x, y in ordereddict.fromkeys(frozenset(x) x in a)] 

and get:

 = [      (('x','y','z',2,3,4), ('a','b','c',5,10,11)),      (('t','f','j',0,1,0), ('h','g','k',2,8,7)),      ...                                         ] 

however if repeat b, getting second tuple selected instead of first:

b = [      (('a','b','c',3,3,2), ('x','y','z',0,0,0)),      (('j','k','l',5,4,3), ('v','t','d',5,10,12)),      ...                                         ] 

ideally b should be:

b = [      (('x','y','z',0,0,0), ('a','b','c',3,3,2)),      (('j','k','l',5,4,3), ('v','t','d',5,10,12)),      ...                                          ]  

i need them same sequence of strings, because using them concatenation of floats in a, b , on. glad know if there way keep selection approach constant deduplicated lists. thanks!

to maintain previous order, iterate on pairs , keep track of have seen. include elements if have not been seen:

def dedup(lst):     seen = set()     result = []     item in lst:         fs = frozenset(item)         if fs not in seen:             result.append(item)             seen.add(fs)     return result 

examples:

>>> = [ ...      (('x','y','z',2,3,4), ('a','b','c',5,10,11)), ...      (('a','b','c',5,10,11), ('x','y','z',2,3,4)), ...      (('t','f','j',0,1,0), ('h','g','k',2,8,7)), ...     ] >>> pprint.pprint(dedup(a)) [(('x', 'y', 'z', 2, 3, 4), ('a', 'b', 'c', 5, 10, 11)),  (('t', 'f', 'j', 0, 1, 0), ('h', 'g', 'k', 2, 8, 7))] >>> b = [ ...      (('x','y','z',0,0,0), ('a','b','c',3,3,2)), ...      (('a','b','c',3,3,2), ('x','y','z',0,0,0)), ...      (('j','k','l',5,4,3), ('v','t','d',5,10,12)), ...     ] >>> pprint.pprint(dedup(b)) [(('x', 'y', 'z', 0, 0, 0), ('a', 'b', 'c', 3, 3, 2)),  (('j', 'k', 'l', 5, 4, 3), ('v', 't', 'd', 5, 10, 12))] 

Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -