排序元组与关系

时间:2013-10-18 22:36:54

标签: python list tuples tie

我正在编写Wilcoxon rank-sums测试的扩展,这要求我首先编写此测试的基本功能。这也意味着我不能使用SciPy进行此练习。

我有基本的骨架代码,但我无法平均关系等级。这是我的代码:

#read in data
m1 = [0,0,0,0,0,2,3,3,3,4,4,5,6,10,10,10,11,12,15,15,15,20,22,25,25,27,30]
w1 = [0,0,0,0,0,0,1,3,3,3,3,7,8,8,19,20,27,30]

#convert to tuples, incl where they came from
m1t = []
for m in m1:
    m1t.append((m, "m1"))
w1t = []
for w in w1:
    w1t.append((w, "w1"))

all1t = m1t + w1t #combine

all1ts = sorted(all1t, key=lambda tup: tup[0]) #sort

all1tsr = [row+(i,) for i,row in enumerate(all1ts,0)] #rank

#revert to back to original grouping
m1r = [i for i in all1tsr if i[1]=="m1"]
w1r = [i for i in all1tsr if i[1]=="w1"]

这是当前的输出:

>>> all1tsr[:15]
[(0, 'm1', 0),
 (0, 'm1', 1),
 (0, 'm1', 2),
 (0, 'm1', 3),
 (0, 'm1', 4),
 (0, 'w1', 5),
 (0, 'w1', 6),
 (0, 'w1', 7),
 (0, 'w1', 8),
 (0, 'w1', 9),
 (0, 'w1', 10),
 (1, 'w1', 11),
 (2, 'm1', 12),
 (3, 'm1', 13),
 (3, 'm1', 14)]

每个元素的元素1是它们的排序值,元素2只是一个标识符,元素3是按元素1排序时的排名。有10个观察值为“0”作为元素1,现在他们都被分配了升序排名,但我想以某种方式平均这些排名(指定所有排名为5)。

换句话说,我想要这个:

[(0, 'm1', 5),
 (0, 'm1', 5),
 (0, 'm1', 5),
 (0, 'm1', 5),
 (0, 'm1', 5),
 (0, 'w1', 5),
 (0, 'w1', 5),
 (0, 'w1', 5),
 (0, 'w1', 5),
 (0, 'w1', 5),
 (0, 'w1', 5),
 (1, 'w1', 11),
 (2, 'm1', 12),
 (3, 'm1', 13.5),
 (3, 'm1', 13.5)]

欢迎所有反馈,谢谢

1 个答案:

答案 0 :(得分:1)

对于初学者来说,我将以更短的方式获得all1ts

import itertools

all1ts = sorted(itertools.chain(((m, "m1") for m in m1),
                                ((w, "w1") for w in w1)))

all1tsr = [row+(i,) for i,row in enumerate(all1ts)]

然后我将使用itertools.groupby,这基本上是为了做这样的事情而设计的。

groups = []
for _, group in itertools.groupby(all1tsr, lambda x: x[0]):
  group = list(group)
  rank = sum(x[2] for x in group) / len(group)
  groups.extend((val, identifier, rank) for val, identifier, _ in group)

运行测试数据可以得到以下结果:

[(0, 'm1', 5),
 (0, 'm1', 5),
 (0, 'm1', 5),
 (0, 'm1', 5),
 (0, 'm1', 5),
 (0, 'w1', 5),
 (0, 'w1', 5),
 (0, 'w1', 5),
 (0, 'w1', 5),
 (0, 'w1', 5),
 (0, 'w1', 5),
 (1, 'w1', 11),
 (2, 'm1', 12),
 (3, 'm1', 16),
 (3, 'm1', 16),
 (3, 'm1', 16),
 (3, 'w1', 16),
 (3, 'w1', 16),
 (3, 'w1', 16),
 (3, 'w1', 16),
 (4, 'm1', 20),
 (4, 'm1', 20),
 (5, 'm1', 22),
 (6, 'm1', 23),
 (7, 'w1', 24),
 (8, 'w1', 25),
 (8, 'w1', 25),
 (10, 'm1', 28),
 (10, 'm1', 28),
 (10, 'm1', 28),
 (11, 'm1', 30),
 (12, 'm1', 31),
 (15, 'm1', 33),
 (15, 'm1', 33),
 (15, 'm1', 33),
 (19, 'w1', 35),
 (20, 'm1', 36),
 (20, 'w1', 36),
 (22, 'm1', 38),
 (25, 'm1', 39),
 (25, 'm1', 39),
 (27, 'm1', 41),
 (27, 'w1', 41),
 (30, 'm1', 43),
 (30, 'w1', 43)]

我认为你想要的是什么。