I have a list 'a'
我有一个列表'a'
a= [(1,2),(1,4),(3,5),(5,7)]
I need to find all the tuples for a particular number. say for 1 it will be
我需要找到特定数字的所有元组。说它会是1
result = [(1,2),(1,4)]
How do I do that?
我怎么做?
8 个解决方案
#1
162
If you just want the first number to match you can do it like this:
如果您只想匹配第一个数字,可以这样做:
[item for item in a if item[0] == 1]
If you are just searching for tuples with 1 in them:
如果您只是搜索其中包含1的元组:
[item for item in a if 1 in item]
#2
80
There is actually a clever way to do this that is useful for any list of tuples where the size of each tuple is 2: you can convert your list into a single dictionary.
实际上有一种聪明的方法可以用于任何元组列表,其中每个元组的大小为2:您可以将列表转换为单个字典。
For example,
例如,
test = [("hi", 1), ("there", 2)]
test = dict(test)
print test["hi"] # prints 1
#3
16
Read up on List Comprehensions
阅读列表理解
[ (x,y) for x, y in a if x == 1 ]
Also read up up generator functions and the yield
statement.
还要读取生成器函数和yield语句。
def filter_value( someList, value ):
for x, y in someList:
if x == value :
yield x,y
result= list( filter_value( a, 1 ) )
#4
7
[tup for tup in a if tup[0] == 1]
#5
5
for item in a:
if 1 in item:
print item
#6
1
>>> [i for i in a if 1 in i]
[(1, 2), (1, 4)]
[(1,2),(1,4)]
#7
0
The filter
function can also provide an interesting solution:
过滤功能还可以提供一个有趣的解决方案:
result = list(filter(lambda x: x.count(1) > 0, a))
which searches the tuples in list for any occurrence of 1
. If the search is limited to the first element, the solution can be modified into:
在列表中搜索任何出现的1的元组。如果搜索仅限于第一个元素,则可以将解决方案修改为:
result = list(filter(lambda x: x[0] == 1, a))
#8
0
Using filter function:
使用过滤功能:
>>> def get_values(iterables, key_to_find):
return list(filter(lambda x:key_to_find in x, iterables)) >>> a = [(1,2),(1,4),(3,5),(5,7)] >>> get_values(a, 1) >>> [(1, 2), (1, 4)]
#1
162
If you just want the first number to match you can do it like this:
如果您只想匹配第一个数字,可以这样做:
[item for item in a if item[0] == 1]
If you are just searching for tuples with 1 in them:
如果您只是搜索其中包含1的元组:
[item for item in a if 1 in item]
#2
80
There is actually a clever way to do this that is useful for any list of tuples where the size of each tuple is 2: you can convert your list into a single dictionary.
实际上有一种聪明的方法可以用于任何元组列表,其中每个元组的大小为2:您可以将列表转换为单个字典。
For example,
例如,
test = [("hi", 1), ("there", 2)]
test = dict(test)
print test["hi"] # prints 1
#3
16
Read up on List Comprehensions
阅读列表理解
[ (x,y) for x, y in a if x == 1 ]
Also read up up generator functions and the yield
statement.
还要读取生成器函数和yield语句。
def filter_value( someList, value ):
for x, y in someList:
if x == value :
yield x,y
result= list( filter_value( a, 1 ) )
#4
7
[tup for tup in a if tup[0] == 1]
#5
5
for item in a:
if 1 in item:
print item
#6
1
>>> [i for i in a if 1 in i]
[(1, 2), (1, 4)]
[(1,2),(1,4)]
#7
0
The filter
function can also provide an interesting solution:
过滤功能还可以提供一个有趣的解决方案:
result = list(filter(lambda x: x.count(1) > 0, a))
which searches the tuples in list for any occurrence of 1
. If the search is limited to the first element, the solution can be modified into:
在列表中搜索任何出现的1的元组。如果搜索仅限于第一个元素,则可以将解决方案修改为:
result = list(filter(lambda x: x[0] == 1, a))
#8
0
Using filter function:
使用过滤功能:
>>> def get_values(iterables, key_to_find):
return list(filter(lambda x:key_to_find in x, iterables)) >>> a = [(1,2),(1,4),(3,5),(5,7)] >>> get_values(a, 1) >>> [(1, 2), (1, 4)]