从元组元组列表中提取元组,其中元组包含字符串

时间:2018-04-26 05:18:31

标签: python list tuples

我有一个元组元组列表,如下所示:

X= [(("a","b","c"),10),(("c","d","e"),20),(("f","e","g"),25)]

我试图提取元组,其中" c"在场。

所以输出应该是

[((("a","b","c"),10),(("c","d","e"),20)]

我正在尝试在python中执行它。  我试过这个

[item for item in X if "c" in item]

然而,它无法正常工作。

在这方面的任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

试试这个:

X= [(("a","b","c"),10),(("c","d","e"),20),(("f","e","g"),25)]
print([item for item in X if "c" in item[0]])

o / p将如下:

[(('a', 'b', 'c'), 10), (('c', 'd', 'e'), 20)]