python - I have a list of tuples and want to make a list that has only one instance of one of the strings in the tuple -


i have list of tuples :

[(1, 'twilight sparkle', 2, 'fluttershy'), (1, 'twilight sparkle', 3, 'applejack'), (1, 'twilight sparkle', 4, 'pinkie pie'), (1, 'twilight sparkle', 5, 'rarity'), (1, 'twilight sparkle', 6, 'rainbow dash'), (1, 'twilight sparkle', 7, 'princess celestia'), (1, 'twilight sparkle', 8, 'princess luna'), (2, 'fluttershy', 3, 'applejack'), (2, 'fluttershy', 4, 'pinkie pie'), (2, 'fluttershy', 5, 'rarity'), (2, 'fluttershy', 6, 'rainbow dash'), (2, 'fluttershy', 7, 'princess celestia'), (2, 'fluttershy', 8, 'princess luna'), (3, 'applejack', 4, 'pinkie pie'), (3, 'applejack', 5, 'rarity'), (3, 'applejack', 6, 'rainbow dash'), (3, 'applejack', 7, 'princess celestia'), (3, 'applejack', 8, 'princess luna'), (4, 'pinkie pie', 5, 'rarity'), (4, 'pinkie pie', 6, 'rainbow dash'), (4, 'pinkie pie', 7, 'princess celestia'), (4, 'pinkie pie', 8, 'princess luna'), (5, 'rarity', 6, 'rainbow dash'), (5, 'rarity', 7, 'princess celestia'), (5, 'rarity', 8, 'princess luna'), (6, 'rainbow dash', 7, 'princess celestia'), (6, 'rainbow dash', 8, 'princess luna'), (7, 'princess celestia', 8, 'princess luna')] 

and want create list has first time string appear, in instance want list be:

[(1, 'twilight sparkle', 2, 'fluttershy'), (3, 'applejack', 4, 'pinkie pie'),(5, 'rarity', 6, 'rainbow dash'), (7, 'princess celestia', 8, 'princess luna')] 

you keep set of elements have been used.

add tuple result if none of elements have been used before:

data = [(1, 'twilight sparkle', 2, 'fluttershy'), (1, 'twilight sparkle', 3, 'applejack'), (1, 'twilight sparkle', 4, 'pinkie pie'), (1, 'twilight sparkle', 5, 'rarity'), (1, 'twilight sparkle', 6, 'rainbow dash'), (1, 'twilight sparkle', 7, 'princess celestia'), (1, 'twilight sparkle', 8, 'princess luna'), (2, 'fluttershy', 3, 'applejack'), (2, 'fluttershy', 4, 'pinkie pie'), (2, 'fluttershy', 5, 'rarity'), (2, 'fluttershy', 6, 'rainbow dash'), (2, 'fluttershy', 7, 'princess celestia'), (2, 'fluttershy', 8, 'princess luna'), (3, 'applejack', 4, 'pinkie pie'), (3, 'applejack', 5, 'rarity'), (3, 'applejack', 6, 'rainbow dash'), (3, 'applejack', 7, 'princess celestia'), (3, 'applejack', 8, 'princess luna'), (4, 'pinkie pie', 5, 'rarity'), (4, 'pinkie pie', 6, 'rainbow dash'), (4, 'pinkie pie', 7, 'princess celestia'), (4, 'pinkie pie', 8, 'princess luna'), (5, 'rarity', 6, 'rainbow dash'), (5, 'rarity', 7, 'princess celestia'), (5, 'rarity', 8, 'princess luna'), (6, 'rainbow dash', 7, 'princess celestia'), (6, 'rainbow dash', 8, 'princess luna'), (7, 'princess celestia', 8, 'princess luna')]  already_added = set() result = []  quad in data:     if not any((x in already_added) x in quad):         x in quad:             already_added.add(x)         result.append(quad)  print(result) # [(1, 'twilight sparkle', 2, 'fluttershy'), (3, 'applejack', 4, 'pinkie pie'), (5, 'rarity', 6, 'rainbow dash'), (7, 'princess celestia', 8, 'princess luna')] 

note code corresponds desired output, not description in question. desired output more restrictive, , elements won't added @ if there coupled seen elements. for:

data = [(1, 'a', 2, 'b'), (1, 'a', 3, 'c'), (2, 'b', 4, 'd')] 

the output be:

[(1, 'a', 2, 'b')] 

no 3, 4, c or d in sight!


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -