【问题标题】:two lists, faster comparison in python两个列表,在 python 中更快的比较
【发布时间】:2016-09-04 15:39:20
【问题描述】:

我正在编写 python (2.7) 脚本来比较两个列表。这些列表是通过读取文件内容从文件创建的。文件只是文本文件,没有二进制文件。文件 1 仅包含哈希(一些明文单词的 MD5 总和),文件 2 是 hash:plain。列表的长度不同(从逻辑上讲,我的“破解”条目比散列少)并且两者都无法排序,因为我必须保持顺序,但这是我想要实现的下一步。到目前为止,我的简单代码如下所示:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
import os

def ifexists(fname):
    if not os.path.isfile(fname):
        print('[-] %s must exist' % fname)
        sys.exit(1)

if len(sys.argv) < 2:
    print('[-] please provide CRACKED and HASHES files')
    sys.exit(1)

CRACKED=sys.argv[1]
HASHES=sys.argv[2]

sk_ifexists(CRACKED)
sk_ifexists(HASHES)

with open(CRACKED) as cracked, open(HASHES) as hashes:
    hashdata=hashes.readlines()
    crackdata=cracked.readlines()
    for c in crackdata:
        for z in hashdata:
            if c.strip().split(':', 1)[0] in z:
                print('found: ', c.strip().split(':', 1))

基本上,我必须将在 HASHES 列表中找到的哈希替换为在 CRACKED 列表中找到的匹配行 hash:plain。我正在遍历 CRACKED,因为它每次都会更短。所以我的问题是,对于较长的列表,上面的代码非常慢。例如,处理两个 60k 行的文本文件最多需要 15 分钟。您对加快速度有何建议?

【问题讨论】:

  • 如何将c.strip().split(':', 1)[0] 拉入一个集合并散列到另一个集合中并寻找两个集合的交集。

标签: python performance python-2.7 comparison


【解决方案1】:

将这些文件之一存储在字典或集合中;取出一个完整的循环,查找平均为 O(1) 常数时间。

例如,看起来crackdata 文件可以轻松转换为字典:

with open(CRACKED) as crackedfile:
    cracked = dict(map(str.strip, line.split(':')) for line in crackedfile if ':' in line)

现在你只需要遍历另一个文件一次

with open(HASHES) as hashes:
    for line in hashes:
        hash = line.strip()
        if hash in cracked:
            print('Found:', hash, 'which maps to', cracked[hash])

【讨论】:

  • 这很棒。现在完成整个操作(60k 条记录)大约需要 1 秒。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多