比较两个列表并在字典中获取匹配结果 - python

时间:2021-02-13 22:08:17

标签: python list dictionary list-comprehension re

我有两个列表,我想找到具有相同/部分字符的项目并将结果放入字典中:

list_a= ['helloyou', 'waithere', 'byenow']

list_b =[ 'wait', 'hello', 'bye']

想要的结果:

dict_c= {'helloyou:hello', 'waithere:wait', 'byenow:bye'}

我试过了,但似乎不起作用:

dict_c= {j:i for i,j in zip(list_a,list_b) if re.match(j,i)} 

编辑:

我可能有些项目的开头不一样,例如:

list_a= ['helloyou', 'waithere', 'byenow']

list_b =[ 'yeswait', 'plushello', 'nobye']

想要的结果:

dict_c= {'helloyou:plushello', 'waithere:yeswait', 'byenow:nobye'}

编辑

如果我可以在这样的情况下使用分隔符来拆分项目并使用开头

list_a = ['hid/jui/helloyou', 'hhh/hdhdh/waithere', 'jcdjcjd/bdcdbc/byenow']

list_b = ['abc/efg/waitai_lp', 'hil/mno/helloai_lj', 'pqr/byeai_ki']

想要的结果

dict_c = {'hid/jui/helloyou:hil/mno/helloai_lj','hhh/hdhdh/waithere:abc/efg/waitai_lp', 'jcdjcjd/bdcdbc/byenow:pqr/byeai_ki'}

3 个答案:

答案 0 :(得分:1)

试试这个 -

问题在于您是 zipping 2 个列表中的相应项目,而不是在它们之间使用 cross-product。因此,在压缩版本中,只有 (bye,byenow) 会从 re.match 返回一些内容。

from itertools import product

{j:i for i,j in product(list_a, list_b) if re.match(j,i)} 
{'hello': 'helloyou', 'wait': 'waithere', 'bye': 'byenow'}

答案 1 :(得分:1)

list_a= ['helloyou', 'waithere', 'byenow']

list_b =[ 'wait', 'hello', 'bye']

dict_c = {a:b for a in list_a for b in list_b if a.startswith(b)}

输出dict_c: {'helloyou': 'hello', 'waithere': 'wait', 'byenow': 'bye'}

答案 2 :(得分:0)

你可以考虑使用here解释的方法,加上@Akshay的答案

from collections import Counter
from itertools import product

def shared_chars(s1, s2):
    return sum((Counter(s1) & Counter(s2)).values())

{j:i for i,j in product(list_a, list_b) if shared_chars(j,i) > 3}

困难的部分是根据一些参数(例如所检查的字符串的长度)以动态方式设置常量值“3”。目前,我认为最短的单词“bye”是最少的