简体   繁体   English

我有一个元组列表,如何取出特定元素?

[英]I have a list of tuples, how can I take out a specific element?

I have a list of tuples like: 我有一个类似的元组列表:

>>>list
[('the', 248),
 ('I', 81),
 ...
 ('I', 81)]

I want to take out a specific element like ('to',248), how should I index the element and get it? 我想取出一个特定元素,例如('to',248),我应该如何索引该元素并获取它?

>>> l =[('the', 248), ('I', 81), ('I', 81)]
>>> x = [i[1] for i in l]
>>> x
[248, 81, 81]

I'm not sure what you exactly mean by this question, but if you want to take an element out with specific qualities, you would just use index() (no matter the data type): 我不确定这个问题到底是什么意思,但是如果您想删除具有特定质量的元素,则只需使用index()(无论数据类型如何):

# x = [("string0",0),("string1",1),("string2",2),("string3",3)]
# Example outputs:
>>> x.index(("string0",0))
0
>>> x.index(("string2",2))
2

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

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