Python:计算2个列表之间的一致性百分比



假设我有2个Python列表xy,长度相同N

一致性定义为,如果 x_i> x_j so y_i> y_j

例如,如果x是学生成绩的列表,而y是毕业后的薪水清单,那么一致性意味着A学生的分数> A学生B-> A将获得比B的收入超过B(并且versa(。

如何以有效的方式计算2个列表之间的一致性百分比(总共有N*(N-1)/2对(?

添加示例:

考虑2个列表:

x = 2,3,1y = 1,2,3

x0<x1和y0<Y1:concordance_count = 1

x0> x2和y0<Y2:concordance_count = 0

x1> x2和y1<Y2:concordance_count = 0

最终,一致性百分比= 1/3

基本上您需要找到x和y

的可能组合
>>> from itertools import combinations                 
>>> x
[2, 3, 1]
>>> y
[1, 2, 3]
>>> combinations(x,2)
<itertools.combinations object at 0x1010482b8>
>>> combinations(y,2)
<itertools.combinations object at 0x101048208>
>>> list(combinations(x,2))
[(2, 3), (2, 1), (3, 1)]
>>> list(combinations(y,2))
[(1, 2), (1, 3), (2, 3)]

,然后在上述列表/迭代器中找到所有值的a<b

>>> ["a<b" if a<b else "a>b" for a,b in combinations(x,2)]
['a<b', 'a>b', 'a>b']
>>> ["a<b" if a<b else "a>b" for a,b in combinations(y,2)]
['a<b', 'a<b', 'a<b']

然后将它们拉开。

此功能返回一个元组列表,其中第i-th元组包含 每个参数序列或迭代的第i-th元素。

注意:这是假设xy的长度相同。如果不是,请使用itertoolsizip_longest

>>> zip(["a<b" if a<b else "a>b" for a,b in combinations(x,2)],["a<b" if a<b else "a>b" for a,b in combinations(y,2)])
[('a<b', 'a<b'), ('a>b', 'a<b'), ('a>b', 'a<b')]

然后找到concordance_count

>>> [1 if v1==v2 else 0 for v1,v2 in zip(('a<b' if a<b else "a>b" for a,b in combinations(x,2)), ('a<b' if a<b else "a>b" for a,b in combinations(y,2)))]
[1, 0, 0]
>>> concordance_count = sum(1 if v1==v2 else 0 for v1,v2 in zip(('a<b' if a<b else "a>b" for a,b in combinations(x,2)), ('a<b' if a<b else "a>b" for a,b in combinations(y,2))))
>>> concordance_count
1
>>> concordance_percentage = concordance_count/max(len(x),len(y))
>>> concordance_percentage
1/3

最新更新