我有一份字典 list ,如下所示:
sample = [{'By': {'email': 'xyz@stackoverflow.com', 'name': 'xyzzy', 'id': '5859'}},{'By': {'email': 'abc@stackoverflow.com', 'name': 'abccb', 'id': '9843'}},
{'By': {'email': 'xyz@stackoverflow.com', 'name': 'xyzzy', 'id': '5859'}}]
From this, I am trying to access keys 'name' and 'id' and write distinct values into a dictionary.
Below is returning id's alone :
print(set(map(lambda x: x['By']['id'], sample)))
output:{‘9843’,‘5859’}
Required output:{"9843":"abccb","5859":"xyzy"}
我try 使用f-字符串将‘id’和‘name’值连接在一起,中间用冒号(:)表示列表中每个词典条目的"id:name"格式.
set(map(lambda x: f"{x['By']['id']}:{x['By']['name']}", sample))
output:{‘9843:abccb’,‘5859:xyzy’}
是否可以访问map lambda函数中两个键的值?谢谢.