如何返回二维列表中的重复项和非重复项

问题描述 投票:0回答:2

我有一个二维列表,其中一些行具有相同的值。 我想从行中的重复项中仅返回重复项和非重复项。 例如,这是我的清单

 df = [[1, 2, 4, 5, 6, 2, 6,7], dup 2, 6
[5, 6, 7, 22, 23, 34, 48], 
[3, 5, 6, 7, 45, 46, 48],
[6, 7, 14, 29, 32, 6, 29], dup 6,29
[6, 7, 13, 23, 33, 35, 7], dup 7
[1, 6, 7, 8, 9, 10, 8],    dup 8
[0, 2, 5, 7, 19, 7, 5]]    dup 5,7

dup = [[2,6],[6,29],[7],[8],[5,7]]

non dups = [[4,5,7],[7,14,32],[6,13,23,33,35],[1,6,7,9,10],[0,2,19]]
python list multidimensional-array pycharm
2个回答
0
投票

根据描述并忽略看似有缺陷的输出,您可以这样做:

from collections import Counter

df = [
    [1, 2, 4, 5, 6, 2, 6, 7],
    [5, 6, 7, 22, 23, 34, 48],
    [3, 5, 6, 7, 45, 46, 48],
    [6, 7, 14, 29, 32, 6, 29],
    [6, 7, 13, 23, 33, 35, 7],
    [1, 6, 7, 8, 9, 10, 8],
    [0, 2, 5, 7, 19, 7, 5],
]

dups = list()
non_dups = list()

for e in df:
    _dups = list()
    _non_dups = list()
    s = set()

    for i, c in Counter(e).items():
        if c > 1:
            if not i in s:
                _dups.append(i)
            s.add(i)
        else:
            _non_dups.append(i)

    if _dups:
        dups.append(_dups)
        if _non_dups:
            non_dups.append(_non_dups)

print(f"{dups=}")
print(f"{non_dups=}")

输出:

dups=[[2, 6], [6, 29], [7], [8], [5, 7]]
non_dups=[[1, 4, 5, 7], [7, 14, 32], [6, 13, 23, 33, 35], [1, 6, 7, 9, 10], [0, 2, 19]]

0
投票

这是一种方法。

代码:

df = [[1, 2, 4, 5, 6, 2, 6,7],
[5, 6, 7, 22, 23, 34, 48], 
[3, 5, 6, 7, 45, 46, 48],
[6, 7, 14, 29, 32, 6, 29],  
[6, 7, 13, 23, 33, 35, 7],  
[1, 6, 7, 8, 9, 10, 8],     
[0, 2, 5, 7, 19, 7, 5]]
dups=[]
non_dups=[]

#traverssing through the list of lists
for list in df:
    #if no duplicates then skip the row/list
    if len(set(list))==len(list):
        continue
    #temp_lists to add duplicates/non-duplicates as a list
    temp_dups=[]
    temp_non_dups=[]
    #traverssing through a row/a "inner list"
    for element in list:
        #if element not already in temp and it's count is >1
        if (element not in temp_dups) and (list.count(element)>1):
            temp_dups.append(element)
        #if element is not a duplicate and not already on the temp_non_dups
        elif (element not in temp_non_dups) and (element not in temp_dups):
            temp_non_dups.append(element)
    #appending temp lists to actual lists
    dups.append(temp_dups)
    non_dups.append(temp_non_dups)
            
print(dups,'\n\n\n',non_dups)

输出:

[[2, 6], [6, 29], [7], [8], [5, 7]] 


[[1, 4, 5, 7], [7, 14, 32], [6, 13, 23, 33, 35], [1, 6, 7, 9, 10], [0, 2, 19]]
© www.soinside.com 2019 - 2024. All rights reserved.