python - 比较python中的两个列表并打印输出

标签 python performance

您好,我有一个列表列表,我需要将每个列表的值与从 XML 文件中提取的另一个列表的值进行比较。结构类似于这样:

[('example', '123', 'foo', 'bar'), ('example2', '456', 'foo', 'bar'), ...]

我需要将每个列表的第二个值与 XML 中的值进行比较:

for item in main_list:
    for child in xml_data:
        if item[4] == child.get('value'):
            print item[4]

问题是 main_list 有大量行(1000 多行),乘以 xml 中的值(100 多行)导致大量迭代使该方法效率低下。

有没有办法有效地做到这一点?

问候。

最佳答案

集合的成员资格检查将比手动迭代和检查快得多:

children = {child.get('value') for child in xml_data}
for item in main_list:
    if item[4] in children:
        print(item[4])

这里我们用一个简单的 set comprehension 构造集合.

请注意,交换集合中的数据可能是值得的 - 如果 main_list 更长,则创建该数据集会更有效率。

items = {item[4] for item in main_list}
for child in xml_data:
    value = child.get('value')
    if value in items:
        print(value)

这两者也只对数据进行一次处理,而不是每次都进行检查。

请注意,集合不会处理集合端的重复值或顺序 - 如果这很重要,则这不是有效的解决方案。此版本将仅使用您正在迭代的数据中的顺序/重复项。如果这无效,那么您仍然可以预先处理数据,并使用 itertools.product()加快迭代速度。

items = [item[4] for item in main_list]
children = [child.get('value') for child in xml_data]

for item, child in itertools.product(items, children):
    if item == child:
        print(item)

正如 Karl Knechtel 指出的那样,如果您真的根本不关心重复项的顺序,您可以只做一个集合交集:

for item in ({child.get('value') for child in xml_data} &
             {item[4] for item in main_list}):
    print(item)

关于python - 比较python中的两个列表并打印输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16689117/

相关文章:

java - Protocol Buffers 中 getSerializedSize() 的性能损失

performance - 电子商务:计算折扣的算法

performance - haskell ; where子句的执行

JavaScript/jQuery html(null) 与 html ('' )

python - 在 Python 中调试问答游戏

python - 如何使用 PySide 加载图像联系表?

Python信息获取实现

php - 使用 file_get_contents vs curl 获取文件大小

python - 在 Flask g 中存储 Postgres 连接

python - 真的可以用 python 发布文件吗?