[英]Access “Actions” object in a list using enumerate() in python
# Python program to illustrate
# enumerate function
l1 = [{'Actions':("eat","sleep","repeat"), 'members':("1028", "jeram", "chilaw")}]
s1 = "geek"
# creating enumerate objects
obj1 = enumerate(l1)
obj2 = enumerate(s1)
print "Return type:",type(obj1)
print list(enumerate(l1['Actions']))
# changing start index to 2 from 0
print list(enumerate(s1,2))
需要使用枚舉 function 在 (l)1 列表中打印“操作”object。 但發生一個錯誤,說列表索引必須是整數,而不是 str。 幫我解決這個問題。
我得到的錯誤是,
Traceback (most recent call last):
File "/home/25b7a4de08d5472b64b462006452cf1f.py", line 11, in <module>
print list(enumerate(l1['Actions']))
TypeError: list indices must be integers, not str
請給我任何解決方案。在此先感謝。
您沒有訪問字典; 索引您的列表,獲取字典。
利用:
list(enumerate(l1[0]['Actions']))
代替:
list(enumerate(l1['Actions']))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.