Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从python中的元组列表中删除唯一元组_Python_Algorithm_Duplicates_Tuples_Uniqueidentifier - Fatal编程技术网

从python中的元组列表中删除唯一元组

从python中的元组列表中删除唯一元组,python,algorithm,duplicates,tuples,uniqueidentifier,Python,Algorithm,Duplicates,Tuples,Uniqueidentifier,我正在编写一个程序来查找重复的文件,现在我有一个元组列表 mylist = [(file1, size1, hash1), (file2, size2, hash2), ... (fileN, sizeN, hashN)] 我想删除具有唯一散列的条目,只留下重复的条目。我正在使用 def dropunique(mylist): templist = [] while mylist: mycandidate = mylist.p

我正在编写一个程序来查找重复的文件,现在我有一个元组列表

mylist = [(file1, size1, hash1),
          (file2, size2, hash2),
          ...
          (fileN, sizeN, hashN)]
我想删除具有唯一散列的条目,只留下重复的条目。我正在使用

def dropunique(mylist):
templist = []
while mylist:
    mycandidate = mylist.pop()
    templist.append([mycandidate])
    for myfile in mylist:
        if myfile[-1] == mycandidate[-1]:
            templist[-1].append(myfile)
            mylist.remove(myfile)
for myfile in templist:
    if len(myfile) != 1:
        mylist.append(myfile)
templist = [item for sublist in mylist for item in sublist]
return templist
在弹出一个条目的地方,查看是否有其他条目具有相同的哈希值和组,然后在具有相同哈希值的列表列表中。然后我用len>1的子列表创建另一个列表,并将结果列表平铺成一个简单列表。
我的问题是,当我在某个列表中使用“for myfile in mylist:”从列表中删除一个条目时,它会跳转相同的条目,然后在后面生存。

将列表复制到以哈希为键的字典中,第二次通过时,删除那些只进行一次计数的代码-您甚至可以使用
集合。计数器
来节省一行或两行代码:

from collections import Counter

counter = Counter(t[2] for t in list_)

result = [value for value in list_ if counter[value[2]] > 1]

(非相关提示:避免将变量命名为“list”或“dict”-这会覆盖Python的默认内置变量)

将列表复制到以散列为键的字典中,然后在第二次遍历时删除那些具有单个计数的变量-您甚至可以使用
集合。计数器
来节省一行或两行代码:

from collections import Counter

counter = Counter(t[2] for t in list_)

result = [value for value in list_ if counter[value[2]] > 1]
(非相关提示:避免将变量命名为“list”或“dict”-这会覆盖这些变量的Python默认内置项)

我将使用a按其哈希值对元组进行分组:

from collections import defaultdict

# Group the tuples by their hashvalues
d = defaultdict(list)
for tup in data:
    filename, size, hashvalue = tup
    d[hash].append(tup)

# Display groups of tuples that have more than one tuple
for hashvalue, tuples in d.items():
    if len(tuples) > 1:
        print('Tuples with %r in common' % hashvalue)
        for tup in tuples:
            print(tup)
        print()
我将使用a按其哈希值对元组进行分组:

from collections import defaultdict

# Group the tuples by their hashvalues
d = defaultdict(list)
for tup in data:
    filename, size, hashvalue = tup
    d[hash].append(tup)

# Display groups of tuples that have more than one tuple
for hashvalue, tuples in d.items():
    if len(tuples) > 1:
        print('Tuples with %r in common' % hashvalue)
        for tup in tuples:
            print(tup)
        print()
使用groupby的解决方案

from itertools import groupby

my_list = [(1, 2, 3),
           (1, 2, 3),
           (4, 5, 6)]


vals = []

for hash_val, items in groupby(sorted(my_list), hash):
    results = tuple(items)
    if len(results) > 1:
        vals.append(results[0])
使用groupby的解决方案

from itertools import groupby

my_list = [(1, 2, 3),
           (1, 2, 3),
           (4, 5, 6)]


vals = []

for hash_val, items in groupby(sorted(my_list), hash):
    results = tuple(items)
    if len(results) > 1:
        vals.append(results[0])
您可以像这样使用double:

filter(lambda el: len(filter(lambda item: item[2] == el[2], my_list)) > 1, my_list)
结果:

>>> my_list = [('file1', 'size1', 'hash1'), ('file2', 'size2', 'hash2'), ('file3', 'size3', 'hash3'), ('file4', 'size4', 'hash2')]
>>>
>>> filter(lambda el: len(filter(lambda item: item[2] == el[2], my_list)) > 1, my_list)
[('file2', 'size2', 'hash2'), ('file4', 'size4', 'hash2')]
请注意,在Python 3中,返回一个迭代器,因此需要将其转换为如下列表:
list(filter(…)

您可以像这样使用double:

filter(lambda el: len(filter(lambda item: item[2] == el[2], my_list)) > 1, my_list)
结果:

>>> my_list = [('file1', 'size1', 'hash1'), ('file2', 'size2', 'hash2'), ('file3', 'size3', 'hash3'), ('file4', 'size4', 'hash2')]
>>>
>>> filter(lambda el: len(filter(lambda item: item[2] == el[2], my_list)) > 1, my_list)
[('file2', 'size2', 'hash2'), ('file4', 'size4', 'hash2')]

请注意,在Python 3中,返回一个迭代器,因此您需要将其转换为如下列表:
list(filter(…)

在迭代列表时删除项目类似于在地球构造板块快速移动时跳跃。请,不要使用
list
作为变量名,因为您会对
list
类型进行阴影处理。例如,替换为
my_list
。在列表中迭代时删除项目类似于在地球构造板块快速移动时跳跃。请不要使用
list
作为变量名,因为您会隐藏
list
类型。例如,替换为
my_list
。不能保证它们的列表是预排序的,因此
groupby
不会生成整个组,因为我们需要更像这样:
key\u fn=operator.itemgetter(1)
其中
1
是“哈希”的索引,然后
itertools.groupby(排序(项,key=key\u fn),key\fn)
不能保证它们的列表是预先排序的,因此
groupby
不会像我们预期的那样组成整个组:
key\u fn=operator.itemgetter(1)
其中
1
是“散列”的索引,然后
itertools.groupby(排序(items,key=key\u fn),key\u fn)
{t[2]:t代表列表中的t
丢弃重复项。此
{t[2]:t代表列表中的t
丢弃重复项。