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

I have this 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 I want to create a list from it, which is only the first time the string appears, in this case I want the list to be:

[(1, 'Twilight Sparkle', 2, 'Fluttershy'), (3, 'Applejack', 4, 'Pinkie
Pie'),(5, 'Rarity', 6, 'Rainbow Dash'), (7, 'Princess Celestia', 8,
'Princess Luna')]

      

+3


source to share


1 answer


You can save set

items that have already been used.

Add a tuple to the result only if none of the 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 = []

for quad in data:
    if not any((x in already_added) for x in quad):
        for 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')]

      

Please note that this code matches the desired output, not the description in your question. This desired result is more restrictive, and some elements will not be added at all if they are always associated with elements already seen. For:



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

      

the output will be:

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

      

No 3

, 4

, c

, or d

in sight!

+2


source







All Articles