【问题标题】:create dictionary of values based on matching keys in list from nested dictionary根据嵌套字典列表中的匹配键创建值字典
【发布时间】:2020-06-20 01:49:29
【问题描述】:

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

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

'TYPE2': [{'Song': 'Jazz', 'Type': 'Slow', 'Price': '5'}], 'TYPE37': [{'Song': 'Country', 'Type': 'Fast', 'Price': '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..]

感谢您的帮助。

【问题讨论】:

    标签: python list dictionary lookup


    【解决方案1】:

    你可以试试这个:

    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)
    

    输出:

    {'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']
    

    【讨论】:

      【解决方案2】:

      使用正则表达式的答案:

      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)
      

      输出:

      {'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'}]}
      

      【讨论】:

      • 感谢您的帮助,这可行,但是当找不到值时,它会给出关键错误。
      猜你喜欢
      • 1970-01-01
      • 2021-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-10
      • 2021-04-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多