我有这样的数据集:
[[0,1],
[0,2],
[0,3],
[0,4],
[1,5],
[1,6],
[1,7],
[2,8],
[2,9]]
我需要删除第一列定义的数据的每个子视图的第一个元素。所以首先我得到第一列中有0的所有元素,并删除第一行:[0,1]。然后我在第一列中得到1的元素并删除第一行[1,5],下一步我删除[2,8],依此类推。最后,我想有一个像这样的数据集:
[[0,2],
[0,3],
[0,4],
[1,6],
[1,7],
[2,9]]
答案 0 :(得分:4)
根据要求,numpy
解决方案:
import numpy as np
a = np.array([[0,1], [0,2], [0,3], [0,4], [1,5], [1,6], [1,7], [2,8], [2,9]])
_,i = np.unique(a[:,0], return_index=True)
b = np.delete(a, i, axis=0)
(上面是编辑以合并@ Jaime的解决方案,这是我为了后人的原始掩蔽解决方案)
m = np.ones(len(a), dtype=bool)
m[i] = False
b = a[m]
有趣的是,面具似乎更快:
In [225]: def rem_del(a):
.....: _,i = np.unique(a[:,0], return_index=True)
.....: return np.delete(a, i, axis = 0)
.....:
In [226]: def rem_mask(a):
.....: _,i = np.unique(a[:,0], return_index=True)
.....: m = np.ones(len(a), dtype=bool)
.....: m[i] = False
.....: return a[m]
.....:
In [227]: timeit rem_del(a)
10000 loops, best of 3: 181 us per loop
In [228]: timeit rem_mask(a)
10000 loops, best of 3: 59 us per loop
答案 1 :(得分:2)
传入您的列表以及要检查值的键。
def getsubset(set, index):
hash = {}
for list in set:
if not list[index] in hash:
set.remove(list)
hash[list[index]] = list
return set
答案 2 :(得分:1)
您希望itertools.groupby()
与itertools.islice()
和itertools.chain
使用{{3}}:
from itertools import islice, chain, groupby
from operator import itemgetter
list(chain.from_iterable(islice(group, 1, None)
for key, group in groupby(inputlist, key=itemgetter(0))))
groupby()
调用将输入列表分组为第一个项目相同的块(itemgetter(0)
是分组键)。 islice(group, 1, None)
调用将组转换为可跳过第一个元素的迭代。chain.from_iterable()
调用会获取每个islice()
结果并将它们链接到一个新的可迭代中,list()
会转回列表。演示:
>>> list(chain.from_iterable(islice(group, 1, None) for key, group in groupby(inputlist, key=itemgetter(0))))
[[0, 2], [0, 3], [0, 4], [1, 6], [1, 7], [2, 9]]
答案 3 :(得分:0)
a = [[0,1],
[0,2],
[0,3],
[0,4],
[1,5],
[1,6],
[1,7],
[2,8],
[2,9]]
a = [y for x in itertools.groupby(a, lambda x: x[0]) for y in list(x[1])[1:]]
print a
答案 4 :(得分:0)
我的回答是:
from operator import itemgetter
sorted(l, key=itemgetter(1)) # fist sort by fist element of inner list
nl = []
[[0, 1], [0, 2], [0, 3], [0, 4], [1, 5], [1, 6], [1, 7], [2, 8], [2, 9]]
j = 0;
for i in range(len(l)):
if(j == l[i][0]):
j = j + 1 # skip element
else:
nl.append(l[i]) # otherwise append in new list
输出是:
>>> nl
[[0, 2], [0, 3], [0, 4], [1, 6], [1, 7], [2, 9]]