[英]Effective way of finding difference between two lists of integers in python
Is there are some effective ways to find a difference between two lists of integers in python?是否有一些有效的方法可以找到 python 中两个整数列表之间的差异? I need to compare a big number of integer lists of the same length with each other and calculation time is critical.我需要比较大量相同长度的 integer 列表,计算时间很关键。 I tried to use pandas, but it had one thing with probably slowed down my calculations: after comparing two series of integers in python it return a list of floats: As an example:我尝试使用 pandas,但它可能会减慢我的计算速度:在比较 python 中的两个整数系列之后,它返回一个浮点列表:例如:
import pandas as pd
from numpy.random import randint
val_series1 = pd.Series(randint(0, 20, 10))
val_series2 = pd.Series(randint(0, 20, 10))
comp_series = val_series1.compare(val_series2)
comp_series
Output:
self other
0 6.0 12.0
1 1.0 12.0
2 17.0 15.0
3 3.0 15.0
5 10.0 5.0
6 17.0 6.0
7 7.0 17.0
8 7.0 14.0
9 18.0 9.0
comp_series.iloc[0]
self 6.0
other 12.0
Name: 0, dtype: float64
After that my future comparison should act with type float or loose time for function.astype(dtype='uint64').在那之后,我未来的比较应该与 function.astype(dtype='uint64') 的类型浮动或松散时间进行比较。
set(val_series1) - set(val_series2)
set
runs really fast in finding differences. set
在发现差异方面运行得非常快。
This can be done in numpy
, consider following simple example这可以在numpy
中完成,考虑以下简单示例
import numpy as np
arr1 = np.array([1,2,3,4,5])
arr2 = np.array([1,1,3,2,5])
arr = np.vstack([arr1,arr2])[:,arr1!=arr2].T
print(arr)
ooutput o输出
[[2 1]
[4 2]]
Explanation: I use 1D arrays rather than Series, I stack them to get 2D array with height 2, then use indexing to get such pair where upper element is not equal lower, then transpose ( .T
) to get output with 2 columns, 1st column hold self, 2nd columns hold other.说明:我使用 1D arrays 而不是 Series,我将它们堆叠以获得高度为 2 的 2D 数组,然后使用索引来获得这样的对,其中上元素不等于更低,然后转置( .T
)以获得具有 2 列的 output,第一列持有自己,第二列持有其他。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.