赞
踩
考虑numpy来解决这个问题:import numpy as np
y = [
[1, 2, 3],
[1, 2, 3],
[3, 4, 5],
[6, 5, 4],
[4, 2, 5],
[4, 2, 5],
[1, 2, 8],
[1, 2, 3]
]
# Returns unique values of array, indices of that
# array, and the indices that would rebuild the original array
unique, indices, inverse = np.unique(y, axis=0, return_index=True, return_inverse=True)
下面是每个变量的打印输出:unique = [
[1 2 3]
[1 2 8]
[3 4 5]
[4 2 5]
[6 5 4]]
indices = [0 6 2 4 3]
inverse = [0 0 2 4 3 3 1 0]
如果我们看看逆变量,可以看到我们确实得到了[0.1.7]作为第一个唯一元素[1 ,2 ,3]的索引位置,我们现在需要做的就是对它们进行适当的分组。new_list = []
for i in np.argsort(indices):
new_list.append(np.where(inverse == i)[0].tolist())
输出:new_list = [[0, 1, 7], [2], [3], [4, 5], [6]]
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。