我有这样一份清单:
mylist[1:3]=[{'Keywords': 'scrum master',
'result': {'categoryId': '3193',
'categoryName': 'agile coach',
'score': '1.0'},
'categoryId': '3193'},
{'Keywords': 'principal consultant',
'result': {'categoryId': '2655',
'categoryName': 'principal consultant',
'score': '1.045369052886963'},
'categoryId': '2655'},
{'Keywords': 'technicalfunctional consultant',
'result': []}]
我想运行以下代码:
categories=set(x['result']['categoryName'] for x in mylist)
它给了我一个错误:
TypeError: list indices must be integers or slices, not str
您必须在开始时定义
mylist
,并为其元素添加if
测试,然后代码工作:关于下面评论中的问题:要使代码正常工作,请在使用变量之前定义变量,并添加另一个
if
条件:相关问题 更多 >
编程相关推荐