比较两个大的 1 和 1 列表的最快方法是什么?0 并返回差异计数/百分比?

What#39;s the fastest way to compare two large lists of 1#39;s amp; 0#39;s and return the difference count/percentage?(比较两个大的 1 和 1 列表的最快方法是什么?0 并返回差异计数/百分比?)

本文介绍了比较两个大的 1 和 1 列表的最快方法是什么?0 并返回差异计数/百分比?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一种方法来快速返回两个大列表之间的差异数.每个列表项的内容为 1 或 0(单个整数),每个列表中的项数始终为 307200.

I'm in need of a method to quickly return the number of differences between two large lists. The contents of each list item is either 1 or 0 (single integers), and the amount of items in each list will always be 307200.

这是我当前代码的示例:

This is a sample of my current code:

list1 = <list1> # should be a list of integers containing 1's or 0's
list2 = <list2> # same rule as above, in a slightly different order

diffCount = 0
for index, item in enumerate(list1):
    if item != list2[index]:
        diffCount += 1

percent = float(diffCount) / float(307200)

上述方法有效,但对于我的目的来说太慢了.我想知道是否有更快的方法来获取列表之间的差异数量,或者差异项目的百分比?

The above works but it is way too slow for my purposes. What I would like to know is if there is a quicker way to obtain the number of differences between lists, or the percentage of items that differ?

我在这个站点上查看了一些类似的线程,但它们的工作方式似乎与我想要的略有不同,并且 set() 示例似乎不适用于我的目的.:P

I have looked at a few similar threads on this site but they all seem to work slightly different from what I want, and the set() examples don't seem to work for my purposes. :P

推荐答案

如果你使用 NumPy,你至少可以获得 10 倍的加速数组而不是列表.

You can get at least another 10X speedup if you use NumPy arrays instead of lists.

import random
import time
import numpy as np
list1 = [random.choice((0,1)) for x in xrange(307200)]
list2 = [random.choice((0,1)) for x in xrange(307200)]
a1 = np.array(list1)
a2 = np.array(list2)

def foo1():
    start = time.clock()
    counter = 0
    for i in xrange(307200):
        if list1[i] != list2[i]:
            counter += 1
    print "%d, %f" % (counter, time.clock()-start)

def foo2():
    start = time.clock()
    ct = np.sum(a1!=a2)
    print "%d, %f" % (ct, time.clock()-start)

foo1() #153490, 0.096215
foo2() #153490, 0.010224

这篇关于比较两个大的 1 和 1 列表的最快方法是什么?0 并返回差异计数/百分比?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

版权声明:本站部分内容来源互联网,如果文章中所涉及的图片或者文字内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

本文标题为:比较两个大的 1 和 1 列表的最快方法是什么?0 并返回差异计数/百分比?

基础教程推荐