【问题标题】:How to access keys in a dictionary with multiple values?如何访问具有多个值的字典中的键?
【发布时间】:2021-06-14 12:12:28
【问题描述】:

这是返回值中包含“10”的字典键的代码。

word_freq = {'is': [1, 3, 4, 8, 10],
         'at': [3, 10, 15, 7, 9],
         'test': [5, 3, 7, 8, 1],
         'this': [2, 3, 5, 6, 11],
         'why': [10, 3, 9, 8, 12]}
# Check if a value exist in dictionary with multiple value
value = 10
# Get list of keys that contains the given value
  list_of_keys = [key
            for key, list_of_values in word_freq.items()
            if value in list_of_values]
if list_of_keys:
   print(list_of_keys)
else:
   print('Value does not exist in the dictionary')

输出['is', 'at', 'why']

我想修改它以查找多个值,例如“10”和“3”,并返回值中包含这些数字的键。

请提出解决方案

【问题讨论】:

  • or命令。

标签: python list dictionary key-value


【解决方案1】:

您可以根据您的用例使用allany

values = [10, 3, 9]
# Get list of keys that contains the given value
list_of_keys = [key
            for key, list_of_values in word_freq.items()
            if all(val in list_of_values for val in values)]

list_of_keys = [key
            for key, list_of_values in word_freq.items()
            if any(val in list_of_values for val in values)]

【讨论】:

    【解决方案2】:

    将值用作数组而不是单个数字。

    word_fre[q = {'is': [1, 3, 4, 8, 10],
             'at': [3, 10, 15, 7, 9],
             'test': [5, 3, 7, 8, 1],
             'this': [2, 3, 5, 6, 11],
             'why': [10, 3, 9, 8, 12]}
    # Check if a value exist in dictionary with multiple value
    value = [10]
    # Get list of keys that contains the given value
    list_of_keys = []
    for i in value:
        for key, list_of_values in word_freq.items():
            if i in list_of_values:
                list_of_keys.append(key)
        
    if list_of_keys:
       print(set(list_of_keys))#this gives all the values only once
    else:
       print('Value does not exist in the dictionary')
    

    【讨论】:

      【解决方案3】:

      使用intersection():

      word_freq = {'is': [1, 3, 4, 8, 10],
               'at': [3, 10, 15, 7, 9],
               'test': [5, 3, 7, 8, 1],
               'this': [2, 3, 5, 6, 11],
               'why': [10, 3, 9, 8, 12]}
      # Check if a value exist in dictionary with multiple value
      values = {10, 3}
      # Get list of keys that contains the given value
        list_of_keys = [key
                  for key, list_of_values in word_freq.items()
                  if values.intersection(set(list_of_values))]
      if list_of_keys:
         print(list_of_keys)
      else:
         print('Values does not exist in the dictionary')
      

      【讨论】:

        【解决方案4】:

        这似乎工作正常。根据需求进行微调。

        word_freq = {'is': [1, 3, 4, 8, 10],
                 'at': [3, 10, 15, 7, 9],
                 'test': [5, 3, 7, 8, 9],
                 'this': [2, 3, 5, 6, 11],
                 'why': [10, 3, 9, 8, 12]}
        
        value = [10, 1]
        
        out = []
        
        for check_val in value :
          print("\nChecking for value : ", check_val)
          for key in word_freq.keys() :
            print("Checking for key : " , key)
            if check_val in word_freq[key] :    
              out.append(key)
          print(out)
        
        print("Final keyset : ", set(out))
        

        输出相同:

        【讨论】:

          猜你喜欢
          • 2020-12-13
          • 2018-09-12
          • 1970-01-01
          • 1970-01-01
          • 2012-12-31
          • 1970-01-01
          • 2011-06-09
          • 2018-06-24
          • 1970-01-01
          相关资源
          最近更新 更多