提问人:Data Analyst 提问时间:4/3/2023 最后编辑:Error - Syntactical RemorseData Analyst 更新时间:4/3/2023 访问量:46
从 Python 中的字典列表中切片字典值 [已关闭]
Slice a dictionary value from list of dictionaries in Python [closed]
问:
我正在尝试将我的字典列表中的特定值附加到另一个列表。
All_Result={'Team Name':[]}
尝试追加
all_result['SupportGroup'].append(issue["fields"]["Team Name"]['objectId'])
这给了我
TypeError:列表索引必须是整数或切片,而不是 str
如何从下面的字典列表中获取以将其附加到?objectId
All_Result
这是我的词典列表
"Team Name" = [{
'workspaceId': 'fdxxxxxxxxxxxxxxxxxxxxxxxxxxx',
'id': 'fdxxxxxxxxxxxxxxxxxx',
'objectId': '1234'
}]
请帮忙
答:
0赞
Mime
4/3/2023
#1
看起来 issue[“fields”] 字典中键“Team Name”的值不是字典本身,而是字典列表。因此,您需要使用索引来访问包含“objectId”键的列表中的字典。
假设 issue[“fields”][“Team Name”] 是字典列表,您可以访问列表中的第一个字典(根据您的示例,这应该是唯一的字典),然后使用键获取“objectId”值。下面是一个示例,说明如何修改代码以将“objectId”值追加到 All_Result['Team Name']:
All_Result = {'Team Name': []}
# Assuming issue["fields"]["Team Name"] is a list of dictionaries
team_dict = issue["fields"]["Team Name"][0]
All_Result['Team Name'].append(team_dict['objectId'])
评论