gpt4 book ai didi

python - 如何区分 2 个非常大的数组?

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:13:13 27 4
gpt4 key购买 nike

我有 2 个非常大的数组。

这段代码运行起来会很慢吗?

results1 = [1,2,3..]results2 = [1,2,3,4 ... ]for result1 in results1:    if result1 not in results2:        print result1

最佳答案

使用集合:

hashed = set(results2)

....

if result1 not in hashed:

请注意,如果您的数组非常大,这需要大量内存。

或者,对两个数组进行排序并使用两个索引。如果两个元素相同,则递增两个索引。如果它们不相等,则增加包含较小元素的数组的索引。

关于python - 如何区分 2 个非常大的数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6937902/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com