问题描述
我有一个元组列表:
[[[('p', 'u'), ('r', 'w')], [('t', 'q')]], [[('p', 'u'), ('r', 'w')], [('v', 'q')]], [[('p', 'u'), ('r', 'w')], [('t', 's')]], [[('p', 'u'), ('r', 'w')], [('v', 's')]], [[('p', 'w'), ('r', 'u')], [('t', 'q')]], [[('p', 'w'), ('r', 'u')], [('v', 'q')]], [[('p', 'w'), ('r', 'u')], [('t', 's')]], [[('p', 'w'), ('r', 'u')], [('v', 's')]], [[('r', 'u'), ('p', 'w')], [('t', 'q')]], [[('r', 'u'), ('p', 'w')], [('v', 'q')]], [[('r', 'u'), ('p', 'w')], [('t', 's')]], [[('r', 'u'), ('p', 'w')], [('v', 's')]], **[[('r', 'w'), ('p', 'u')], [('t', 'q')]]**, [[('r', 'w'), ('p', 'u')], [('v', 'q')]], [[('r', 'w'), ('p', 'u')], [('t', 's')]], [[('r', 'w'), ('p', 'u')], [('v', 's')]]]
但是现在例如元素[[('p','u'),('r','w')], [('t','q')]]
与[[('r','w'),('p','u')], [('t','q')]]
相同,在列表中标记为 fat .
is the same as [[('r','w'),('p','u')], [('t','q')]]
, which are marked fat in the list.
因此,在列表中,我有16个元素,其中每个元素都是双精度的.
So in the list I have 16 elements, where every element is double.
现在,我要删除重复项,只剩下前八个元素.
Now, I want to delete the duplicates, that I have only the first eight elements left.
所以我很天真地尝试过
[[list(y) for y in set([tuple(set(x)) for x in doublegammas1])]]
但是在这里,他说:
TypeError: unhashable type: 'list'
所以我的问题:
如何扩展列表理解功能,使其适用于更多维的列表?
How can I extend the list comprehension, that it works for a more dimensional list?
推荐答案
可变对象(例如列表或集合)不能是集合的成员.您可以使用不可变的Frozenset.
A mutable object (such as a list or a set) cannot be a member of a set. You can use a frozenset, which is immutable.
main_list = [[[('p', 'u'), ('r', 'w')], [('t', 'q')]],
[[('p', 'u'), ('r', 'w')], [('v', 'q')]],
[[('p', 'u'), ('r', 'w')], [('t', 's')]],
[[('p', 'u'), ('r', 'w')], [('v', 's')]],
[[('p', 'w'), ('r', 'u')], [('t', 'q')]],
[[('p', 'w'), ('r', 'u')], [('v', 'q')]],
[[('p', 'w'), ('r', 'u')], [('t', 's')]],
[[('p', 'w'), ('r', 'u')], [('v', 's')]],
[[('r', 'u'), ('p', 'w')], [('t', 'q')]],
[[('r', 'u'), ('p', 'w')], [('v', 'q')]],
[[('r', 'u'), ('p', 'w')], [('t', 's')]],
[[('r', 'u'), ('p', 'w')], [('v', 's')]],
[[('r', 'w'), ('p', 'u')], [('t', 'q')]],
[[('r', 'w'), ('p', 'u')], [('v', 'q')]],
[[('r', 'w'), ('p', 'u')], [('t', 's')]],
[[('r', 'w'), ('p', 'u')], [('v', 's')]]]
main_set = set(tuple(frozenset(innermost_list) for innermost_list in sublist) for sublist in main_list)
from pprint import pprint
pprint(main_set)
输出:
{(frozenset({('r', 'u'), ('p', 'w')}), frozenset({('t', 'q')})),
(frozenset({('p', 'u'), ('r', 'w')}), frozenset({('v', 'q')})),
(frozenset({('r', 'u'), ('p', 'w')}), frozenset({('v', 'q')})),
(frozenset({('p', 'u'), ('r', 'w')}), frozenset({('t', 's')})),
(frozenset({('r', 'u'), ('p', 'w')}), frozenset({('t', 's')})),
(frozenset({('p', 'u'), ('r', 'w')}), frozenset({('v', 's')})),
(frozenset({('r', 'u'), ('p', 'w')}), frozenset({('v', 's')})),
(frozenset({('p', 'u'), ('r', 'w')}), frozenset({('t', 'q')}))}
要转换回嵌套列表的原始结构,请执行以下操作:
To convert back to the original structure of nested lists:
new_list = [[list(frozen) for frozen in subtuple] for subtuple in main_set]
pprint(new_list)
输出:
[[[('r', 'u'), ('p', 'w')], [('t', 'q')]],
[[('p', 'u'), ('r', 'w')], [('v', 'q')]],
[[('r', 'u'), ('p', 'w')], [('v', 'q')]],
[[('p', 'u'), ('r', 'w')], [('t', 's')]],
[[('r', 'u'), ('p', 'w')], [('t', 's')]],
[[('p', 'u'), ('r', 'w')], [('v', 's')]],
[[('r', 'u'), ('p', 'w')], [('v', 's')]],
[[('p', 'u'), ('r', 'w')], [('t', 'q')]]]
更新:
一种从输入数据中就地删除重复项的解决方案.
A solution that removes the duplicate items in-place from the input data.
unique = []
for item in main_list[:]:
frozen_item = frozenset(frozenset(innermost_list) for innermost_list in item)
if frozen_item not in unique:
unique.append(frozen_item)
else:
main_list.remove(item)
这篇关于如果相同,则删除多维列表中的元组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!