查找在数组中只出现一次的项目

时间:2012-02-12 22:57:00

标签: python arrays tuples python-2.7

我有一个二维数组。在这种情况下,每个行向量被认为是感兴趣的量。我想要做的是将所有恰好一次的行作为一个数组返回,并将所有出现多次的行作为第二个数组返回。

例如,如果数组是:

a=[[1,1,1,0], [1,1,1,0], [5,1,6,0], [3,2,1,0], [4,4,1,0], [5,1,6,0]]

我想返回两个数组:

nonsingles=[[1,1,1,0], [1,1,1,0], [5,1,6,0], [5,1,6,0]]
singles= [[3,2,1,0], [4,4,1,0]]

保持订单的重要性。我写的代码如下:

def singles_nonsingles(array):
#returns the elements that occur only once, and the elements
#that occur more than once in the array
singles=[]
nonsingles=[]
arrayhash=map(tuple, array)

for x in arrayhash:
    if (arrayhash.count(x)==1):
        singles.append(x)

    if (arrayhash.count(x)>1):
        nonsingles.append(x)

nonsingles=array(nonsingles)
singles=array(singles)

return {'singles':singles, 'nonsingles':nonsingles}

现在,我很高兴地说,这个工作,但不快地说,它是极其缓慢,作为一个典型的阵列我有是30000(行)×10元件/行= 300000个元件。任何人都可以给我一些关于如何加快这一点的提示?如果这个问题很简单,我很抱歉,我是Python新手。此外,我正在使用Numpy / Scipy和Python 2.7,如果有任何帮助的话。

4 个答案:

答案 0 :(得分:3)

在Python 2.7或更高版本中,您可以使用collections.Counter来计算出现次数:

def unique_items(iterable):
    tuples = map(tuple, iterable)
    counts = collections.Counter(tuples)
    unique = []
    non_unique = []
    for t in tuples:
        if counts[t] == 1:
            unique.append(t)
        else:
            non_unique.append(t)
    return unique, non_unique

答案 1 :(得分:2)

我认为您的问题是您正在对in进行list测试。这具有O(n)性能。

构建dict然后使用它来弄清楚如何处理每一行应该更快。

编辑:代码中有一个不必要的enumerate();我把它剥掉了。

from collections import defaultdict

def singles_nonsingles(array):
    #returns the elements that occur only once, and the elements
    #that occur more than once in the array
    singles=[]
    nonsingles=[]
    d = defaultdict(int)

    t = [tuple(row) for row in array]

    for row in t:
        d[row] += 1

    for row in t:
        if d[row] == 1:
            singles.append(row)
        else:
            nonsingles.append(row)

    return {'singles':singles, 'nonsingles':nonsingles}

这是一个只返回唯一行的版本:

from collections import defaultdict

def singles_nonsingles(array):
    #returns the elements that occur only once, and the elements
    #that occur more than once in the array
    singles=[]
    nonsingles=[]
    d = defaultdict(int)
    already_seen = set()

    t = [tuple(row) for row in array]

    for row in t:
        d[row] += 1

    for row in t:
        if row in already_seen:
            continue
        if d[row] == 1:
            singles.append(row)
        else:
            nonsingles.append(row)
        already_seen.add(row)

    return {'singles':singles, 'nonsingles':nonsingles}


a=[[1,1,1,0], [1,1,1,0], [5,1,6,0], [3,2,1,0], [4,4,1,0], [5,1,6,0]]

x = singles_nonsingles(a)
print("Array: " + str(a))
print(x)

答案 2 :(得分:0)

第一个只返回没有重复的单个/无单个数组的列表,第二个返回重复

def comp (multi):
    from collections import defaultdict

    res = defaultdict(int)

    for vect in multi:
        res[tuple(vect)] += 1

    singles = []
    no_singles = []

    for k in res:
        if res[k] > 1:
            no_singles.append(list(k))
        elif res[k] == 1:
            singles.append(list(k))

    return singles, no_singles

def count_w_repetitions(multi):
    from collections import defaultdict

    res = defaultdict(int)

    for vect in multi:
        res[tuple(vect)] += 1

    singles = []
    no_singles = []

    for k in res:
        if res[k] == 1:
            singles.append(list(k))
        else:
            for i in xrange(res[k]):
                no_singles.append(list(k))


    return singles, no_singles

答案 3 :(得分:0)

from itertools import compress,imap

def has_all_unique(a):
   return len(a) == len(frozenset(a)) 

uniq = map( has_all_unique,a)
singles = list(compress(a,uniq))
notuniq = imap(lambda x: not x,uniq)
nonsingles = list(compress(a,notuniq))