python - 在 python 中使用 enumerate() 访问列表中的“Actions”对象
问题描述
# 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))
需要使用 enumerate 函数在 (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 - 在多核上运行 keras API 多输入/输出
- r - UseMethod(“select_”)中的Rmarkdown错误退出错误:没有适用于“select_”的适用方法应用于类“函数”的对象
- node.js - 如何使用电报机器人回复引用消息?
- resilience4j - 如何在 Resilience4J 中实现多个具有相同配置的断路器
- angular - 通过 formGroup + ngModel 使用的角度反应式表单验证:弃用的致命警告 + 移除支持?
- owl - 从“.owl”文件导入命名空间并在 RDFLIB 图中使用术语
- rest - 在 ElasticSearch RestHighLevelClient 对象中出现超时错误。如何延长30000毫秒的时间限制?
- python - 将 emty 列表 `[]` 转换为 `[0]` 熊猫数据框
- reactjs - google 没有在 @react-google-maps/api 的 Gatsby js 应用程序中定义
- jestjs - Jest:编写所有需要首先断言某些步骤的测试的正确方法是什么?