简体   繁体   English

将 Python 列表值与字典键进行比较

[英]Comparing Python list values to dictionary keys

I am trying to create a new list by comparing the list items to dictionary keys, if the match is found then i append a value of dictionary to new list that was globally initialized.我正在尝试通过将列表项与字典键进行比较来创建一个新列表,如果找到匹配项,那么我将 append 字典的值与全局初始化的新列表进行比较。 the code i am trying is as below我正在尝试的代码如下

dict_1 = {'OMSS': '10.1.1.0/24', 'A&A': '10.1.2.0/24', 'AFM': '10.1.3.0/24', 'ATM': '10.1.4.0/24'}
list_1 = ['A&A', 'A&A', 'OMSS', 'OMSS', 'A&A', 'AFM', 'A&A', 'AFM', 'A&A']
list_2 = ['OMSS ', 'OMSS ', 'A&A ', 'A&A ', 'AFM ', 'A&A ', 'AFM ', 'A&A ', 'AFM ']

list_of_list1 = []

for s1 in list_1:
    #print (s2)
    for key, value in dict_1.items():
        #print (key)
        if s1 == key:
            list_of_list1.append(value)

print (list_of_list1)

list_of_list2 = []

for s2 in list_2:
    #print (s2)
    for key, value in dict_1.items():
        #print (key)
        if s2 == key:
            list_of_list2.append(value)

print (list_of_list2)

when i run this i get below output当我运行这个时,我得到低于 output

['10.1.2.0/24', '10.1.2.0/24', '10.1.1.0/24', '10.1.1.0/24', '10.1.2.0/24', '10.1.3.0/24', '10.1.2.0/24', '10.1.3.0/24', '10.1.2.0/24']
{'OMSS': '10.1.1.0/24', 'A&A': '10.1.2.0/24', 'AFM': '10.1.3.0/24', 'ATM': '10.1.4.0/24'}
[]

i am trying to figure out why "list_of_list2" is coming out as empty?我想弄清楚为什么“list_of_list2”是空的?

  1. Please stop using containers in most inefficient way.请停止以最低效的方式使用容器。 Just use list comprehension:只需使用列表理解:
list_of_list1 = [dict_1[k] for k in list_1 if k in dict_1]
  1. All items of list_2 have ' ' at the end. list_2的所有项目末尾都有' ' Indeed they won't be equal to the keys you have in dict_1 .实际上,它们不会等于您在dict_1中拥有的键。 Try to strip redundant spaces:尝试去除多余的空格:
list_of_list2 = [dict_1[k.strip()] for k in list_2 if k.strip() in dict_1]

You need to remove leading and tailing space from the list_2 items to be matched with the dictionary keys.您需要从list_2项中删除前导和尾随空间以与字典键匹配。 Here is the sample fix of your problem:这是您的问题的示例修复:

dict_1 = {'OMSS': '10.1.1.0/24', 'A&A': '10.1.2.0/24', 'AFM': '10.1.3.0/24', 'ATM': '10.1.4.0/24'}
list_1 = ['A&A', 'A&A', 'OMSS', 'OMSS', 'A&A', 'AFM', 'A&A', 'AFM', 'A&A']
list_2 = ['OMSS ', 'OMSS ', 'A&A ', 'A&A ', 'AFM ', 'A&A ', 'AFM ', 'A&A ', 'AFM ']

list_of_list1 = []

for s1 in list_1:
    #print (s2)
    for key, value in dict_1.items():
        #print (key)
        if s1 == key:
            list_of_list1.append(value)

print (list_of_list1)

list_of_list2 = []

for s2 in list_2:
    #print (s2)
    for key, value in dict_1.items():
        #print (key)
        if s2.strip() == key:
            list_of_list2.append(value)

print (list_of_list2)

Output: Output:

['10.1.2.0/24', '10.1.2.0/24', '10.1.1.0/24', '10.1.1.0/24', '10.1.2.0/24', '10.1.3.0/24', '10.1.2.0/24', '10.1.3.0/24', '10.1.2.0/24']
['10.1.1.0/24', '10.1.1.0/24', '10.1.2.0/24', '10.1.2.0/24', '10.1.3.0/24', '10.1.2.0/24', '10.1.3.0/24', '10.1.2.0/24', '10.1.3.0/24']

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

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