获取与另一个给定列表的每个项匹配的键列表…?

huangapple go评论85阅读模式
英文:

Get a list of keys that match each item of another given list...?

问题

  1. result_list = [key for key, values in my_dict.items() if any(item in values for item in search_list)]
英文:
  1. my_dict = {
  2. 'NONE': ['N', 'NE'],
  3. 'VERY SLIGHT': ['VSLT', 'VT'],
  4. 'SLIGHT': ['SLT', 'ST'],
  5. 'FAINT': ['F', 'FT'],
  6. 'MEDIUM': ['M', 'MM'],
  7. 'STRONG': ['S', 'SG'],
  8. 'VERY STRONG': ['VS', 'VG']
  9. }
  10. search_list = ['N', 'SLT', 'MM']
  11. result_list = []

I want to search each item of search_list in values of my_dict and return the matching keys as list.

I need... result_list = ['NONE', 'SLIGHT', 'MEDIUM']

I got it by...

  1. for i in search_list:
  2. for k, v in my_dict.items():
  3. if i in v:
  4. result_list.append(k)

how to get it in more simpler and quickest way to do it?

答案1

得分: 1

将文本到代码的字典翻转为代码到文本的字典(每个程序运行一次),然后只需进行简单快速的字典查找。

英文:

"Flip" your text-to-codes dict to a code-to-text dict (once per your program), and then it's just a simple, fast dict lookup away.

  1. text_to_codes = {
  2. "NONE": ["N", "NE"],
  3. "VERY SLIGHT": ["VSLT", "VT"],
  4. "SLIGHT": ["SLT", "ST"],
  5. "FAINT": ["F", "FT"],
  6. "MEDIUM": ["M", "MM"],
  7. "STRONG": ["S", "SG"],
  8. "VERY STRONG": ["VS", "VG"],
  9. }
  10. code_to_text = {}
  11. for text, codes in text_to_codes.items():
  12. code_to_text.update({code: text for code in codes})
  13. # `code_to_text` will end up looking like
  14. # {'N': 'NONE', 'NE': 'NONE', 'VSLT': 'VERY SLIGHT', 'VT': 'VERY SLIGHT', 'SLT': 'SLIGHT', ...
  15. # at this point.
  16. search_list = ["N", "SLT", "MM"]
  17. result_list = [code_to_text[code] for code in search_list]
  18. print(result_list) # ['NONE', 'SLIGHT', 'MEDIUM']

答案2

得分: 0

嵌套推导是一种选择。

  1. [k for k, v in my_dict.items() for x in search_list if x in v]
英文:

Nested comprehension is one option.

  1. [k for k, v in my_dict.items() for x in search_list if x in v]

答案3

得分: 0

Sure, here's the translated code:

  1. search_list = ['N', 'SLT', 'MM']
  2. find_list = []
  3. for k, v in my_dict.items():
  4. find_list.extend(map(lambda x: (x, k), v))
  5. dict_result = {tup[0]: tup[1:] for tup in find_list}
  6. for i in search_list:
  7. print(dict_result[i])

Please note that I've translated the code as requested, but I recommend using a more meaningful variable name than "dict" to avoid conflicts with the built-in Python dict type.

英文:
  1. search_list = ['N', 'SLT', 'MM']
  2. find_list=[]
  3. for k,v in my_dict.items():
  4. find_list.extend(map(lambda x: (x,k), v))
  5. dict = {tup[0]: tup[1:] for tup in find_list}
  6. for i in search_list:
  7. print(dict[i])

答案4

得分: -1

You could use list comprehension:

  1. search_list = ["N", "SLT", "MM"]
  2. result_list = [k for k, v in my_dict.items() for x in search_list if x in v]

This adds the appropriate keys to the result list.
(Add a key only if one of its items is the abbreviation you want.)

On the other hand, you could flip your dictionary, then just look it up:

  1. code_to_text = {}
  2. for k, v in text_to_codes.items():
  3. code_to_text.update({code: k for code in v})
  4. search_list = ["N", "SLT", "MM"]
  5. result_list = [code_to_text[code] for code in search_list]

Both of these give the same value of result_list:

  1. ['NONE', 'SLIGHT', 'MEDIUM']
英文:

You could use list comprehension:

  1. search_list = ["N", "SLT", "MM"]
  2. result_list = [k for k, v in my_dict.items() for x in search_list if x in v]

This adds the appropriate keys to the result list.
(Add a key only if one of its items is the abbreviation you want.)

On the other hand, you could flip your dictionary, then just look it up:

  1. code_to_text = {}
  2. for k, v in text_to_codes.items():
  3. code_to_text.update({code: k for code in v})
  4. search_list = ["N", "SLT", "MM"]
  5. result_list = [code_to_text[code] for code in search_list]

Both of these give the same value of result_list:

  1. ['NONE', 'SLIGHT', 'MEDIUM']

huangapple
  • 本文由 发表于 2023年5月10日 18:19:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76217261.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定