gpt4 book ai didi

python - 我有一个元组列表,想要创建一个列表,该列表只有元组中的一个字符串的一个实例

转载 作者:行者123 更新时间:2023-11-28 17:17:52 25 4
gpt4 key购买 nike

我有这个元组列表:

[(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')]

我想从中创建一个列表,只有第一次出现的字符串,在这种情况下,我希望列表是:

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

最佳答案

您可以保留一组已使用过的元素的

仅当之前没有使用过任何元素时才向结果添加一个元组:

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')]

请注意,此代码对应于所需的输出,而不是您问题中的描述。这种期望的输出更具限制性,如果总是耦合到已经看到的元素,则根本不会添加某些元素。对于:

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

输出将是:

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

看不到 34cd!

关于python - 我有一个元组列表,想要创建一个列表,该列表只有元组中的一个字符串的一个实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43277128/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com