简体   繁体   English

在Python列表中的元组中搜索字符串

[英]Search for a string in a tuple inside a Python list

I have a list in Python, 我有一份Python list

a=[("title","artiste"),
   ("titl","artste"),
   ("ttle","titl")]

I want to create a second list; 我想创建第二个列表; the second list should not include the search term. 第二个列表不应包含搜索词。

How to search whether "titl" is in the first string of the tuples (just the first string, not the second string) of the three tuples or not? 如何搜索"titl"是否在三个元组的第一个字符串中(只是第一个字符串,不是第二个字符串)?

Use any() and a generator expression to determine whether the first ( [0] ) element of any of the tuples ( t ) in list a equal 'titl' : 使用any()和一个发电机的表达 ,以确定是否所述第一( [0]任何一个的)元件的元组tin list a等于'titl'

>>> any((t[0] == 'titl' for t in a))
True

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM