简体   繁体   English

sum()是否可以像哈希一样使用

[英]Can sum() be used like a hash

I want to check whether the sum of two list is same or not. 我想检查两个列表的总和是否相同。

lst1 = [1 2 3 4]
lst2 = [0 3 3 4]
if sum(lst1) == sum(lst2):
    return true

Here the sum returns true. 在这里,总和返回true。 If I hash the list I get different values but hashing is computationally expensive. 如果我对列表进行哈希处理,则会得到不同的值,但哈希计算在计算上是昂贵的。 I only want to check whether the elements in the two lists are same (only with the exception of 1 element) . 我只想检查两个列表中的元素是否相同(仅1个元素除外) I am using Binary search technique of dividing the list and then checking the hash. 我正在使用二进制搜索技术划分列表,然后检查哈希。 if hashes are different I am checking if its more than once. 如果哈希值不同,我正在检查是否多次。 But as I said hashing is computationally expensive. 但是正如我所说,哈希在计算上是昂贵的。 Also the order does matter here . 这里的顺序也很重要 Thank you 谢谢

First, is is not the logically correct syntax for your application; 首先, is不是为您的应用逻辑上正确的语法; use == instead. 使用==代替。 Refer this post to know more about the difference: Is there a difference between `==` and `is` in Python? 请参阅这篇文章,以了解更多区别: Python中`==`和`is`之间是否有区别?

def checksum(lst1, lst2):
    return sum(lst1) == sum(lst2):

list1 = [1, 2, 3, 4]
list2 = [0, 3, 3, 4]
checksum(list1,list2)

This is the correct code to compare a checksum (and is obviously true in this example). 比较校验和的正确代码(在此示例中显然是正确的)。

If you are looking for something different, like finding whether the items of the list are the same element-wise, then the straight-forward answer would be 如果您正在寻找不同的东西,例如查找列表项是否在元素方面相同,那么直接答案将是

return lst1 == lst2

is checks for object identity, not equality ( link ). is检查对象身份,而不是相等性( link )。

sum does not differentiate between the elements that are summed over only the end result. sum不会区分仅对最终结果求和的元素。 If you need to differentiate between the elements of the containers you can use a set 如果需要区分容器的元素,可以使用一set

set(lst1) == set(lst2) # ignores counts of elements

or if you're sensitive to the counts of the elements use collections.Counter 或者如果您对元素的数量敏感,则使用collections.Counter

collections.Counter(lst2) == collections.Counter(lst1) # False
collections.Counter(lst1) == collections.Counter(lst1) # True

this requires you to iterate over the list. 这需要您遍历列表。


Depending on what exactly you're after you could also you 根据您所追求的精确程度,您也可以

hash(tuple(lst1)) == hash(tuple(lst2))

This checks both the elements contained and their order (maybe you don't care about order) and is pretty efficient. 这将检查包含的元素及其顺序(也许您不关心顺序),并且效率很高。 You have to do the cast to a tuple as list is not immutable and therefore can not be hashed. 您必须对tuple因为list不是一成不变的,因此不能进行哈希处理。

hash(tuple) seems to be much faster, but it's not clear to me what exactly your end goal is hash(tuple)似乎要快得多,但我不清楚您的最终目标是什么

%timeit hash(tuple(lst1)) == hash(tuple(lst2))
1000000 loops, best of 3: 408 ns per loop

%timeit collections.Counter(lst2) == collections.Counter(lst1)
100000 loops, best of 3: 5.58 µs per loop

Can sum be used like a hash? sum可以像哈希一样使用吗?

Yes, the sum() function can be used as a hash for a list or tuple of integers. 是的, sum()函数可用作整数列表或元组的哈希。

Is hashing is computationally expensive? 哈希计算在计算上是否昂贵?

Interestingly, hash() performs better than sum() for a tuple of integers : 有趣的是, 对于整数元组hash()的性能优于sum()

$ python2.7 -m timeit -s "data = (1, 2, 3, 4)" "sum(data)"
10000000 loops, best of 3: 0.113 usec per loop

$ python2.7 -m timeit -s "data = (1, 2, 3, 4)" "hash(data)"
10000000 loops, best of 3: 0.0569 usec per loop

How to compare unordered lists 如何比较无序列表

The stated problem is "how to compare elements in the two lists are same (only with the exception of 1 element)" 陈述的问题是“如何比较两个列表中的元素相同(仅1个元素除外)”

A built-in tool for doing this is collections.Counter() . 一个内置的工具是collections.Counter() Internally, it uses fast hashing to accumulate counts. 在内部,它使用快速散列来累加计数。 In Python 3, it is accelerated by code written in C. It uses a single O(n) pass rather than an O(log n) binary search. 在Python 3中,用C语言编写的代码对其进行了加速。它使用一次O(n)传递,而不是O(log n)二进制搜索。

>>> from collections import Counter
>>> list1 = [1, 2, 3, 4]
>>> list2 = [0, 3, 3, 4]

>>> c1 = Counter(list1)
>>> c2 = Counter(list2)
>>> c1 == c2
False
>>> c1 - c2
Counter({1: 1, 2: 1})
>>> c2 - c1
Counter({0: 1, 3: 1})

The difference in the various approaches will become more pronounced as the list size grows larger. 随着列表大小的增加,各种方法中的差异将变得更加明显。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM