简体   繁体   中英

Python: List containing tuple and integer

I have a list that is a combination of a tuple and integer

ex: K = [(7,8),8]

How do I access the first element of the tuple, 7?

I tried K[0[0]] and was not successful.

Try doing the following:

    >>> k = [(7,8),8]
    >>> k[0]
    (7, 8)
    >>> k[0][0]
    7

Accessing element like this in a collection is wrong - K[0[0]]

In this case, you are indexing the 0th element of 0 itself, which definitely causes an error. Try doing k[0][0]

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