python - 最快检查行是否以列表中的值开头?

标签 python performance filter

我有数以千计的值(作为列表,但如果有帮助,可能会转换为字典等)并且想要与具有数百万行的文件进行比较。我想要做的是将文件中的行过滤为仅以列表中的值开头的行

最快的方法是什么?

我的慢代码:

  for line in source_file:
    # Go through all IDs
    for id in my_ids:
      if line.startswith(str(id) + "|"):
        #replace comas with semicolons and pipes with comas
        target_file.write(line.replace(",",";").replace("|",","))

最佳答案

如果您确定该行以 id +“|”和“|”开头不会出现在 id 中,我想你可以用“|”玩一些把戏。例如:

my_id_strs = map(str, my_ids)
for line in source_file:
    first_part = line.split("|")[0]
    if first_part in my_id_strs:
        target_file.write(line.replace(",",";").replace("|",","))

希望这会有所帮助:)

关于python - 最快检查行是否以列表中的值开头?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33623518/

相关文章:

python - 将字典值传递给哈希函数

mysql - 子查询和连接之间的性能?

c - 为什么以 null 结尾的字符串?或者: null-terminated vs.个字符+长度存储

java - 如何实现适配器并过滤结果?

image-processing - OpenCV中是否有其他方法可以进行形态学操作?

python - 如何根据每个类别的频率对 Pandas 数据框进行子采样?

python - 如何为变量值分配标签

python - 在 Python 中测试 SSL v3 支持

multithreading - Qt GUI 中许多小部件的快速更新

ios - 不确定如何使用 CICrop 裁剪图像