如何根据python中的前两个元素计算嵌套列表中的重复项

时间:2014-06-02 17:38:37

标签: python list

我的表格中有一个列表:

lst = [[1, 0, 0, 0], [1, 1, 0, 0], [2, 0, 0, 0], [2, 1, 0, 0], [2, 1, 0, 0], [1, 1, 0, 0], [3, 1, 0, 0], [1, 3, 0, 0], [2, 1, 0, 0], [2, 0, 0, 0]]

但是最后两个子元素在开始时总是为零,所以它可能是:

lst = [[1, 0], [1, 1], [2, 0], [2, 1], [2, 1], [1, 1], [3, 1], [1, 3], [2, 1], [2, 0]]

如果这更容易。

我想要的是删除并计算此列表的重复项,并将第3个子元素设置为计数,这样如果我们采用上述我想要的:

lst = [[1, 0, 1, 0], [1, 1, 2, 0], [2, 0, 2, 0], [2, 1, 3, 0], [3, 1, 1, 0], [1, 3, 1, 0]]

我找到了解释如何删除重复项: Removing Duplicates from Nested List Based on First 2 ElementsRemoving duplicates from list of lists in Python

但我不知道如何计算重复数。整个列表中元素的顺序并不重要,但子列表中元素的顺序必须保留,因为[1,3]和[3,1]不是同一个东西。

如果事实证明这是一个死胡同,我可以做一些像哈希前两个元素的计数,但只有在计数后才能让它们回来。

感谢任何帮助。 对不起诵读困难!

4 个答案:

答案 0 :(得分:1)

例如:

lst = [[1, 0, 0, 0], [1, 1, 0, 0], [2, 0, 0, 0], [2, 1, 0, 0], [2, 1, 0, 0], [1, 1, 0, 0], [3, 1, 0, 0], [1, 3, 0, 0], [2, 1, 0, 0], [2, 0, 0, 0]]

from collections import Counter

c = Counter(tuple(i) for i in lst)

print [list(item[0][0:2] + (item[1], 0)) for item in c.items()]

# [[1, 0, 1, 0], [1, 1, 2, 0], [3, 1, 1, 0], [2, 1, 3, 0], [1, 3, 1, 0], [2, 0, 2, 0]]

答案 1 :(得分:0)

详细说明njzk2提供的重要提示:

  • 将您的列表列表转换为元组列表
  • 从中创建一个计数器
  • 从柜台获取一个字典
  • 将子列表的第3个元素设置为计数器

    的频率
    from collections import Counter
    lst = [[1, 0, 0, 0], [1, 1, 0, 0], [2, 0, 0, 0], [2, 1, 0, 0], [2, 1, 0, 0], [1, 1, 0, 0], [3, 1, 0, 0], [1, 3, 0, 0], [2, 1, 0, 0], [2, 0, 0, 0]]
    list_of_tuples = [tuple(elem) for elem in lst]
    dct = dict(Counter(list_of_tuples))
    lst = [list(e) for e in dct]
    for elem in lst:
        elem[2] = dct[tuple(elem)]
    

编辑:删除了for循环之前的行的重复项。之前没有看到过这个要求。

答案 2 :(得分:0)

您可以这样做以保持重复次数:

lst = [[1, 0], [1, 1], [2, 0], [2, 1], [2, 1], [1, 1], [3, 1], [1, 3], [2, 1], [2, 0]]

for x in lst:
    count = 1
    tmpLst = list(lst)
    tmpLst.remove(x)
    for y in tmpLst:
        if x[0] == y[0] and x[1] == y[1]:
            count = count + 1
    x.append(count)
    #x.append(0) #if you want to add that 4th element

print lst

结果:

[[1, 0, 1], [1, 1, 2], [2, 0, 2], [2, 1, 3], [2, 1, 3], [1, 1, 2], [3, 1, 1], [1, 3, 1], [2, 1, 3], [2, 0, 2]]

然后您可以lstremove duplicates as mentioned in the link you posted.

答案 3 :(得分:0)

一种不同的(可能是功能性的)方法。

lst = [[1, 0, 0, 0], [1, 1, 0, 0], [2, 0, 0, 0], [2, 1, 0, 0],\
       [2, 1, 0, 0], [1, 1, 0, 0], [3, 1, 0, 0], [1, 3, 0, 0],\
       [2, 1, 0, 0], [2, 0, 0, 0]]  

def rec_counter(lst):
    # Inner method that is called at the end. Receives a
    # list, the current element to be compared and an accumulator
    # that will contain the result.
    def counter(lst, elem, acc):
        new_lst = [x for x in lst if x != elem]
        elem[2] = lst.count(elem)
        acc.append(elem)
        if len(new_lst) == 0:
            return acc
        else:
            return counter(new_lst, new_lst[0], acc)
    # This part starts the recursion of the inner method. If the list
    # is empty, nothing to do. Otherwise, count starting with the first
    # element of the list and an empty accumulator.
    if len(lst) == 0:
        return []
    else:
        return counter(lst, lst[0], [])

print rec_counter(lst)
# [[1, 0, 1, 0], [1, 1, 2, 0], [2, 0, 2, 0], \
#  [2, 1, 3, 0], [3, 1, 1, 0], [1, 3, 1, 0]]