from itertools import chain
main = ['dayn is the one', 'styn is a main', 'tyrn is the third main']
lst2 = ['dayz', 'stzn', 'tyrm']
lst3 = ['styzerwe', 'tyrmadsf', 'dayttt']
lst4 = ['dayl', 'styyzt', 'tyrl']
def create_dict(main, match=3, *rest):
result = {item[:match]:[item, []] for item in main}
result['unmatched'] = ['unmatched', []]
for item in chain(*rest):
(result.get(item[:match]) or result['unmatched'])[1].append(item)
return dict(result.values())
result = create_dict(main, 3, lst2, lst3, lst4)
print(result)
输出:
{'dayn is the one': ['dayz', 'dayttt', 'dayl'],
'styn is a main': ['styzerwe', 'styyzt'],
'tyrn is the third main': ['tyrm', 'tyrmadsf', 'tyrl'],
'unmatched': ['stzn']}