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

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

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

问题

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

search_list = ['N', 'SLT', 'MM']

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...

for i in search_list:
    for k, v in my_dict.items():
        if i in v:
            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.

text_to_codes = {
    "NONE": ["N", "NE"],
    "VERY SLIGHT": ["VSLT", "VT"],
    "SLIGHT": ["SLT", "ST"],
    "FAINT": ["F", "FT"],
    "MEDIUM": ["M", "MM"],
    "STRONG": ["S", "SG"],
    "VERY STRONG": ["VS", "VG"],
}
code_to_text = {}
for text, codes in text_to_codes.items():
    code_to_text.update({code: text for code in codes})

# `code_to_text` will end up looking like
#   {'N': 'NONE', 'NE': 'NONE', 'VSLT': 'VERY SLIGHT', 'VT': 'VERY SLIGHT', 'SLT': 'SLIGHT', ...
# at this point.

search_list = ["N", "SLT", "MM"]
result_list = [code_to_text[code] for code in search_list]
print(result_list)  # ['NONE', 'SLIGHT', 'MEDIUM']

答案2

得分: 0

嵌套推导是一种选择。

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

Nested comprehension is one option.

[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:

search_list = ['N', 'SLT', 'MM']
find_list = []
for k, v in my_dict.items():
    find_list.extend(map(lambda x: (x, k), v))

dict_result = {tup[0]: tup[1:] for tup in find_list}

for i in search_list:
    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.

英文:
search_list = ['N', 'SLT', 'MM']
find_list=[]
for k,v in my_dict.items():
    find_list.extend(map(lambda x: (x,k), v))

dict = {tup[0]: tup[1:] for tup in find_list}

for i in search_list:
    print(dict[i])

答案4

得分: -1

You could use list comprehension:

search_list = ["N", "SLT", "MM"]
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:

code_to_text = {}

for k, v in text_to_codes.items():
    code_to_text.update({code: k for code in v})

search_list = ["N", "SLT", "MM"]
result_list = [code_to_text[code] for code in search_list]

Both of these give the same value of result_list:

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

You could use list comprehension:

search_list = ["N", "SLT", "MM"]
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:

code_to_text = {}

for k, v in text_to_codes.items():
    code_to_text.update({code: k for code in v})

search_list = ["N", "SLT", "MM"]
result_list = [code_to_text[code] for code in search_list]

Both of these give the same value of result_list:

['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:

确定