Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python比较两个列表,如果存在匹配项,则替换原始列表_Python_List_Python 3.x - Fatal编程技术网

python比较两个列表,如果存在匹配项,则替换原始列表

python比较两个列表,如果存在匹配项,则替换原始列表,python,list,python-3.x,Python,List,Python 3.x,我有两个包含字符串的列表。我想获取基本列表中的每个项目,并搜索查看自定义列表中任何值的前3个字符是否匹配。如果存在匹配项,则将base\u列表中的原始值替换为custom\u列表中的值。如果不匹配,则保留原始值 base_list = ["abc123", "cde123", "efg456", "ghi123"] custom_list = ["abc321", "efg654"] 期望输出: modified_base_list = ["abc321", "cde123", "efg65

我有两个包含字符串的列表。我想获取
基本列表
中的每个项目,并搜索查看
自定义列表
中任何值的前3个字符是否匹配。如果存在匹配项,则将
base\u列表
中的原始值替换为
custom\u列表
中的值。如果不匹配,则保留原始值

base_list = ["abc123", "cde123", "efg456", "ghi123"]

custom_list = ["abc321", "efg654"]
期望输出:

modified_base_list = ["abc321", "cde123", "efg654", "ghi123"]
最后,我还想将这个新的
modified_base_list
编写为一个包含项目的文件,每行一个

我试过:

modified_base_list = []

for custom in custom_list:
    for base in base_list:
        if custom[:3] == base[:3]:
            modified_base_list.append(custom)
        else:
            modified_base_list.append(base)


print(modified_base_list)

with open('newfile.txt', 'w') as f:
    for s in modified_base_list:
        f.write(s)

***编辑问题以说明具有15k+行的列表,以找到更快的方法。这是一种改变原始列表的解决方案,仅替换存在所需匹配项的列表:

>>> base_list = ["abc123", "cde123", "efg456", "ghi123"]
>>> custom_list = ["abc321", "efg654"]
>>> for i, x in enumerate(base_list):
        for test in custom_list:
            if test[:3] == x[:3]:
                base_list[i] = test
                break

>>> base_list
['abc321', 'cde123', 'efg654', 'ghi123']
当然,如果您不想修改原始列表,可以先使用
modified_base_list=base_list[:]
创建它的副本


您也可以遵循自己的想法,但在这种情况下,您必须确保您主要是在
base_list
上迭代,并且不会多次添加项目:

modified_base_list = []
for base in base_list:
    found = False
    for custom in custom_list:
        if custom[:3] == base[:3]:
            modified_base_list.append(custom)
            found = True
            break

    if not found:
        modified_base_list.append(base)
您也可以在此处使用“
for…else”
,而不是找到的实用程序变量“

for base in base_list:
    for custom in custom_list:
        if custom[:3] == base[:3]:
            modified_base_list.append(custom)
            break
    else:
        modified_base_list.append(base)

您可以使用包含生成器表达式的列表:

base_list = ["abc123", "cde123", "efg456", "ghi123"]
custom_list = ["abc321", "efg654"] 
modified_base_list = [next((y for y in custom_list if y[:3] == x[:3]), x) for x in base_list]
# ['abc321', 'cde123', 'efg654', 'ghi123']
请注意,我假设如果相同的3个字符前缀在
自定义\u列表中多次出现,则您只希望使用第一个实例。

尝试以下操作:

输出:

>>> res
['**abc321**', 'cde123', '**efg654**', 'ghi123']

为此,您可以使用和的组合:

base_list = ["abc123", "cde123", "efg456", "ghi123"]

custom_list = ["abc321", "efg654"]

smaller_custom = [y[:3] for y in custom_list]

modified_base_list = ["**{}**".format(custom_list[smaller_custom.index(x[:3])]) if x[:3] in smaller_custom else x for x in base_list]
# ['**abc321**', 'cde123', '**efg654**', 'ghi123']

with open('output_data.txt','w') as outfile:
    outfile.write("\n".join(modified_base_list))

我希望这有帮助。

输出是什么?应该是什么?
[“**{}**”。如果映射中的x[:3],则格式化(x)(lambda y:y[:3],自定义列表)否则x代表基本列表中的x]
。您确定他要修改
基本列表
?在他的代码中,他创建了一个新的列表。@leaf我在问题标题后面写着“替换原始列表”。但是如果你不想这样做,你可以很容易地先创建一个副本,然后开始。谢谢你在这方面的帮助。有没有什么方法可以加快这个大名单的速度?我的列表是通过解析15000行以上的文件创建的。哪个是大列表?无论如何,您必须完全迭代
base\u list
,因此无法加快迭代速度。如果您的
自定义\u列表非常大,您可以通过先创建这些行的查找来优化它,这样您就可以在固定时间内匹配它们,而无需反复循环。您好,Abdou,结果列表仅标记原始列表中具有匹配项的项目,但我需要替换在自定义列表中发现的前3个字符匹配的实际值。您能解释一下“next”语句在做什么吗?@JiaLi
next
查找生成器表达式为
True
的第一个匹配项,如果没有出现,则默认值设置为
x
base_list = ["abc123", "cde123", "efg456", "ghi123"]

custom_list = ["abc321", "efg654"]

smaller_custom = [y[:3] for y in custom_list]

modified_base_list = ["**{}**".format(custom_list[smaller_custom.index(x[:3])]) if x[:3] in smaller_custom else x for x in base_list]
# ['**abc321**', 'cde123', '**efg654**', 'ghi123']

with open('output_data.txt','w') as outfile:
    outfile.write("\n".join(modified_base_list))