简体   繁体   中英

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?

>>> 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):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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