我有一个元组列表,并希望创建一个列表,其中只有一个元组中的一个字符串的实例

时间:2017-04-07 11:35:55

标签: python tuples

我有这个元组列表:

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

我想从它创建一个只有第一次出现字符串的列表,在这个例子中我希望列表为:

rng = np.random.RandomState(42)
df = pd.DataFrame(rng.randint(0, 10, (3, 4)),
              columns=['A', 'B', 'C', 'D'])
np.set_printoptions(precision=4,suppress=True)
data = np.sin(df * np.pi / 4)
print(data)

1 个答案:

答案 0 :(得分:2)

您可以保留已使用过的set个元素。

只有在以前没有使用过任何元素时才向结果添加元组:

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