使用 python 中的 enumerate() 访问列表中的 "Actions" 对象

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

需要使用枚举函数打印 (l)1 列表中的 "Action" 个对象。但是出现一个错误,说列表索引必须是整数,而不是 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

有什么办法提前给我please.Thanks

您没有访问字典;索引你的列表,获取字典。

使用:

list(enumerate(l1[0]['Actions']))

而不是:

list(enumerate(l1['Actions']))