简体   繁体   English

如何在元组数组中找到特定的元组?

[英]How to find specific tuple inside array of tuples?

I got an array of tuples, each tuple contains (string, string), and I want to find specific tuple inside the array.我有一个元组数组,每个元组包含 (string, string),我想在数组中找到特定的元组。

example:例子:

[("command","ABCDEFG"),("arguments","XYZW"),("time","ERRTY")]

使用in关键字:

('foo', 'bar') in [('foo', 'bar'),('spam', 'eggs')]

You can convert these tuples to dict and search by first element:您可以将这些元组转换为dict并按第一个元素搜索:

list_of_tuples = [("command","ABCDEFG"),("arguments","XYZW"),("time","ERRTY")]

d = dict(list_of_tuples)

print(d['arguments'])

Prints:印刷:

XYZW

Edit: to check if some key exists in dictionary, use in operator, eg.:编辑:要检查字典中是否存在某个键,请in运算符中使用,例如:

if 'arguments' in d:
    print(d['arguments'])
else:
    print('Not found!')

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

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