gpt4 book ai didi

python - Python 中比较字符串元素的最快方法

转载 作者:太空宇宙 更新时间:2023-11-03 18:29:05 29 4
gpt4 key购买 nike

我正在寻找在 python 中比较字符串元素的最快方法。

import os, glob, numpy as np

with open ('fname.txt','r') as fi: ##This infile contains 9 thousands of string elements
all_list = fi.read().splitlines()

existing_list = glob.glob('*jpg') ##This contains 5 thousands elements
existing_list = [os.path.basename(f) for f in existing_list]

remaining_list = [f for f in all_list if f not in existing_list]
for i in remaining list:
print i

如何在 Numpy 中执行它?

all_list = np.array(all_list)
existing_list = np.array(existing_list)
remaining_list = ???

最佳答案

如果您使用集合,则可以在不使用 numpy 的情况下对此进行优化:

existing_set = {os.path.basename(f) for f in existing_list}  # set comprehension, python2.7+
# alternatively: set(os.path.basename(f) for f in existing_list)

remaining_list = [f for f in all_list if f not in existing_set]

我怀疑即使你找到了一种方法,使用 numpy 是否会获得很多性能......

关于python - Python 中比较字符串元素的最快方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22692656/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com