社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

从与条件python匹配的列表创建字典

John Taylor • 3 年前 • 1168 次点击  

鉴于这些样本清单

main = ['dayn is the one', 'styn is a main', 'tyrn is the third main']
lst2 = ['dayz', 'stzn', 'tyrm']
lst3 = ['stywerwe', 'tyrmadsf', 'dayttt']

我试图创建一个字典,它将主列表中的每个元素都作为键,并且只将那些与主列表中任何值的前三个字符匹配的元素,以及与lst2或lst3中的任何值匹配的元素作为该键的值的列表。

我尝试了几个版本,但都没用。

matched = {}

for x in main:
    for y in lst2:
        if x[:3] == y[:3]:
            matched[x] = y

这段代码让我很接近,但不完全符合这个结果:

{'dayn is the one': 'dayz', 'tyrn is the third main': 'tyrm'}

我的实际数据是我公司的四个不同命名地点的列表。最初的列表是这些位置的专有名称,其他三个来自三个不同来源的列表是这样创建的,这些作者使用了这些名称的缩写版本,等等。因此,如果我能匹配主列表和其他三个列表的前5个字符,我可以创建一个映射字典来纠正其他三个源中那些设施的非常规命名版本。预期产出如下:

示例列表项目:

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']

预期结果:

{'dayn is the one':['dayz','dayttt', 'dayl'],'styn is a main':['styzerwe', 'styyzt'],'tyrn is the third main':['tyrm', 'tyrmadsf', 'tyrl']} 

我们的目标是使用上述字典,然后通过将其用作pandas中的映射对象,更正任何数据帧中设施名称的任何版本。在所有各种命名约定中,前5个左右的字符是相同的,是确保匹配唯一名称的一种方法。

我研究了更新字典、有序字典和python中的默认字典,但没有任何东西可以解决这个难题。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/128631
 
1168 次点击  
文章 [ 3 ]  |  最新文章 3 年前
AziMez
Reply   •   1 楼
AziMez    3 年前

几句话 .

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']

keys= tuple(main)
data= tuple(lst2+lst3+lst4)
elem=  [[e for e in data if e.startswith(keys[i][:3])] for i in(range(3))]
result= dict(zip(keys, elem))

print(result)

[ 输出 ]

{'dayn is the one': ['dayz', 'dayttt', 'dayl'], 'styn is a main': ['styzerwe', 'styyzt'], 'tyrn is the third main': ['tyrm', 'tyrmadsf', 'tyrl']}
buran
Reply   •   2 楼
buran    3 年前
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']}
Andrej Kesely
Reply   •   3 楼
Andrej Kesely    3 年前

尝试:

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"]


tmp = {}
for l in [lst2, lst3, lst4]:
    for v in l:
        tmp.setdefault(v[:3], []).append(v)

out = {v: tmp.get(v[:3], []) for v in main}
print(out)

印刷品:

{
    "dayn is the one": ["dayz", "dayttt", "dayl"],
    "styn is a main": ["styzerwe", "styyzt"],
    "tyrn is the third main": ["tyrm", "tyrmadsf", "tyrl"],
}