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

标签 python indexing enumerate

# 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

请给我任何解决方案。提前致谢。

最佳答案

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

用途:

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

而不是:

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

关于python - 在python中使用enumerate()访问列表中的 "Actions"对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58822417/

相关文章:

python - 如何匹配文本和可选的重复标识符并将它们组合在一起?

python - .NET Core 2.0 和 Angular 初始应用程序构建失败 - 找不到 python 后跟 JavaScript 运行时错误

Python:查找列表字段之间的距离

python - 即使使用 1 行/2 列数组,也在 python 中迭代 Numpy 数组行

python id3 标签 unicode

python - 将文字字符串从 python-vim 脚本发送到 tmux Pane

python - 根据给定索引交换两个列表的值

mongodb - Mongo 稀疏索引未按预期工作

MySQL 在表连接中不使用索引

具有枚举列表的 Python(递归?)函数正在遍历未知数据