[英]Check list of tuples where first element of tuple is specified by defined string
This question is similar to Check that list of tuples has tuple with 1st element as defined string but no one has properly answered the "wildcard" question. 这个问题类似于检查元组列表是否具有第一个元素作为定义字符串的元组,但没有人正确回答“通配符”问题。
Say I have [('A', 2), ('A', 1), ('B', 0.2)]
说我有[('A', 2), ('A', 1), ('B', 0.2)]
And I want to identify the tuples where the FIRST element is A. How do I return just the following? 我想确定FIRST元素为A的元组。如何返回以下内容?
[('A', 2), ('A', 1)]
Using a list comprehension: 使用列表理解:
>>> l = [('A', 2), ('A', 1), ('B', 0.2)]
>>> print([el for el in l if el[0] == 'A'])
[('A', 2), ('A', 1)]
Simple enough list comprehension: 简单的列表理解:
>>> L = [('A', 2), ('A', 1), ('B', 0.2)]
>>> [(x,y) for (x,y) in L if x == 'A']
[('A', 2), ('A', 1)]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.