繁体   English   中英

根据嵌套字典列表中的匹配键创建值字典

[英]create dictionary of values based on matching keys in list from nested dictionary

我有嵌套字典,其中包含从 TYPE1 到 TYPE300 的多达 300 个项目,称为 mainlookup

mainlookup = {'TYPE1': [{'Song': 'Rock', 'Type': 'Hard', 'Price': '10'}],

'TYPE2': [{'Song': 'Jazz', 'Type': 'Slow', 'Price': '5'}], 'TYPE37': [{'Song': 'Country', 'Type': '快速','价格':'7'}]}

根据字符串 TYPE1、TYPE2 等进行查找的输入列表

input_list = ['thissong-fav-user:type1-chan-44-John', 
'thissong-fav-user:type1-chan-45-kelly-md', 
'thissong-fav-user:type2-rock-45-usa',
'thissong-fav-user:type737-chan-45-patrick-md', 
'thissong-fav-user:type37-chan-45-kelly-md']

我想找到字符串 TYPE IN input_list 然后创建一个字典,如下所示

Output_Desired = {'thissong-fav-user:type1-chan-44-John': [{'Song': 'Rock', 'Type': 'Hard', 
'Price':'10'}],

'thissong-fav-user:type1-chan-45-kelly-md': [{'Song': 'Rock', 'Type': 'Hard', 'Price': '10'}], 'thissong-fav -user:type2-rock-45-usa': [{'Song': 'Jazz', 'Type': 'Slow', 'Price': '5'}], 'thissong-fav-user:type37-chan -45-kelly-md': [{'Song': 'Country', 'Type': 'Fast', 'Price': '7'}]}

Note-thissong-fav-user:type737-chan-45-patrick-md in the list has no match so i want to create a 
seperate list if value is not found in main lookup

Notfound_list = ['thissong-fav-user:type737-chan-45-patrick-md', and so on..]

感谢你的帮助。

你可以试试这个:

mainlookup = {'TYPE1': [{'Song': 'Rock', 'Type': 'Hard', 'Price': '10'}],
'TYPE2': [{'Song': 'Jazz', 'Type': 'Slow', 'Price': '5'}], 'TYPE37': [{'Song': 'Country', 'Type': 'Fast', 'Price': '7'}]}

input_list = ['thissong-fav-user:type1-chan-44-John', 
'thissong-fav-user:type1-chan-45-kelly-md', 'thissong-fav-user:type737-chan-45-kelly-md']



dct={i:mainlookup[i.split(':')[1].split('-')[0].upper()] for i in input_list if i.split(':')[1].split('-')[0].upper() in mainlookup.keys()}

Notfoundlist=[i for i in input_list if i not in dct.keys() ]
print(dct)
print(Notfoundlist)

Output:

{'thissong-fav-user:type1-chan-44-John': [{'Song': 'Rock', 'Type': 'Hard', 'Price': '10'}], 'thissong-fav-user:type1-chan-45-kelly-md': [{'Song': 'Rock', 'Type': 'Hard', 'Price': '10'}]}
['thissong-fav-user:type737-chan-45-kelly-md']

使用正则表达式的答案:

import re
from pprint import pprint

input_list = ['thissong-fav-user:type1-chan-44-John', 'thissong-fav-user:type1-chan-45-kelly-md', 'thissong-fav-user:type2-rock-45-usa', 'thissong-fav-user:type737-chan-45-patrick-md', 'thissong-fav-user:type37-chan-45-kelly-md']
mainlookup = {'TYPE2': {'Song': 'Reggaeton', 'Type': 'Hard', 'Price': '30'}, 'TYPE1': {'Song': 'Rock', 'Type': 'Hard', 'Price': '10'}, 'TYPE737': {'Song': 'Jazz', 'Type': 'Hard', 'Price': '99'}, 'TYPE37': {'Song': 'Rock', 'Type': 'Soft', 'Price': '1'}}

pattern = re.compile('type[0-9]+')
matches = [re.search(pattern, x).group(0) for x in input_list]
result = {x: [mainlookup[matches[i].upper()]] for i, x in enumerate(input_list)}
pprint(result)

Output:

{'thissong-fav-user:type1-chan-44-John': [{'Price': '10',
                                           'Song': 'Rock',
                                           'Type': 'Hard'}],
 'thissong-fav-user:type1-chan-45-kelly-md': [{'Price': '10',
                                               'Song': 'Rock',
                                               'Type': 'Hard'}],
 'thissong-fav-user:type2-rock-45-usa': [{'Price': '30',
                                          'Song': 'Reggaeton',
                                          'Type': 'Hard'}],
 'thissong-fav-user:type37-chan-45-kelly-md': [{'Price': '1',
                                                'Song': 'Rock',
                                                'Type': 'Soft'}],
 'thissong-fav-user:type737-chan-45-patrick-md': [{'Price': '99',
                                                   'Song': 'Jazz',
                                                   'Type': 'Hard'}]}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM