[英]Index of an item within a list within a list?
說我有以下代碼:
names_list = [['Abby', 'Albert'], ['Bert', 'Bob'], ['Gina', 'Greg']]
現在你怎么打印阿爾伯特? 打印'abby','albert'我會用:
print (names_list[0])
你會如何使用列表中的項目? 希望你明白我的意思。
print (names_list[0][1]) # prints Albert
要在評論中回答這個問題:
names_list = [['Abby', ['name', 'lastname']]]
print(names_list[0][1][1]) # prints lastname
| 0 | # names_list[0]
names_list = [['Abby', ['name', 'lastname']]]
| 0 | | 1 | # two elems within names_list[0]
| 0 | | 1 | # two elems in names_list[0][1]
你可以分2步完成
Python 3.3.1 (default, Sep 25 2013, 19:29:01)
[GCC 4.7.3] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> names_list = [['Abby', 'Albert'], ['Bert', 'Bob'], ['Gina', 'Greg']]
>>>
>>> firstset = names_list[0]
>>> print (firstset)
['Abby', 'Albert']
>>>
>>> albert = firstset[1]
>>> print (albert)
Albert
或者你可以一步到位
>>> print (names_list[0][1])
Albert
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.