英文:
Unique sublists in Python
问题
我有一个包含许多子列表的列表D
。我想识别唯一的子列表并移除重复的子列表。这里的唯一意味着无论特定元素的位置如何,所有元素都相同。但是我遇到了一个错误。以下是期望的输出。
D = [[0, 2, 3, 5], [1, 3, 4, 6], [2, 0, 3, 5], [4, 1, 3, 6]]
unique_sublists = []
for sublist in D:
if sublist not in unique_sublists:
unique_sublists.append(sublist)
print(unique_sublists)
错误是
in <module>
E=D[i].unique
AttributeError: 'list' object has no attribute 'unique'
期望的输出是
[[0, 2, 3, 5], [1, 3, 4, 6]]
英文:
I have a list D
containing many sublists. I want to identify unique sublists and remove the repeating ones. By unique, I mean all the elements are the same irrespective of the locations of specific elements. But I am getting an error. I present the expected output.
D=[[0, 2, 3, 5], [1, 3, 4, 6], [2, 0, 3, 5], [4, 1, 3, 6]]
for i in range(0,len(D)):
E=D[i].unique
print(E)
The error is
in <module>
E=D[i].unique
AttributeError: 'list' object has no attribute 'unique'
The expected output is
[[0, 2, 3, 5], [1, 3, 4, 6]]
答案1
得分: 3
你尚未准确定义“unique”的含义。如果子列表中没有重复项,那么可以执行类似以下操作:
def unique_sublists(data):
seen = set()
result = []
for sub in data:
uniq = frozenset(sub)
if uniq not in seen:
result.append(sub)
seen add(uniq)
return result
如果子列表中可以有重复项,可能需要类似以下操作:
def unique_sublists(data):
from collections import Counter
seen = set()
result = []
for sub in data:
uniq = frozenset(Counter(sub).items())
if uniq not in seen:
result.append(sub)
seen add(uniq)
return result
英文:
You haven't defined what you mean by "unique" precisely enough. If there are no repeats in the sublists, then you can do something like:
def unique_sublists(data):
seen = set()
result = []
for sub in data:
uniq = frozenset(sub)
if uniq not in seen:
result.append(sub)
seen.add(uniq)
return result
If there can be repeats, you might need something like:
def unique_sublists(data):
from collections import Counter
seen = set()
result = []
for sub in data:
uniq = frozenset(Counter(sub).items())
if uniq not in seen:
result.append(sub)
seen.add(uniq)
return result
答案2
得分: 1
你可以使用 set
来获取唯一的项。如果不关心顺序是否改变,你可以使用列表推导,对每个子列表进行排序,将其转换为元组以便进行哈希,然后最终将其转换为集合以去除重复项。
unique = set(tuple(sorted(a)) for a in D)
# 转换回列表
unique = [list(a) for a in unique]
如果你关心顺序,可以做类似的事情,但是创建一个字典以获取原始子列表。
unique = dict(zip([tuple(sorted(a)) for a in D], D)).values()
# 转换回列表
unique = [list(a) for a unique]
英文:
You can use set
to get unique items. If you don't care if order is changed, you can use list comprehension that sorts each sublist, converts it to a tuple so that it can be hashed, and then finally convert it to a set to remove duplicates.
unique = set(tuple(sorted(a)) for a in D)
# convert back to lists
unique = [list(a) for a in unique]
If you do care about order, you can do a similar thing except make a dictionary to get the original sublist.
unique = dict(zip([tuple(sorted(a)) for a in D], D)).values()
# convert back to lists
unique = [list(a) for a in unique]
答案3
得分: 0
- 使用
map
来排序列表,然后使用set
来移除重复的项。
list(set(map(lambda x: tuple(sorted(x)), D)))
- 这里使用临时列表,仅在项尚未添加时才添加。
out = []
for i in map(sorted, D):
if i not in out: out.append(i)
英文:
There are multiple ways of doing this, I will add the two I came up with:
- Using map to order the lists and then set to remove the duplicated ones.
list(set(map(lambda x: tuple(sorted(x)), D)))
-
Here using a temporary list to add only if the items have not been added yet.
out = [] for i in map(sorted, D): if i not in out: out.append(i)
答案4
得分: 0
对于Python列表,没有名为'unique'的方法,因此出现错误。要识别唯一的子列表,你可以将子列表转换为元组,创建一个元组集合来删除重复项,然后将元组转换回列表。你可以按照以下方式解决这个问题:
D = [[0, 2, 3, 5], [1, 3, 4, 6], [2, 0, 3, 5], [4, 1, 3, 6]]
# 对子列表元素进行排序(如果在你的情况下元素顺序不重要)
unique_tuples = set(tuple(sorted(sublist)) for sublist in D)
unique_sublists = [list(t) for t in unique_tuples]
print(unique_sublists)
[[0, 2, 3, 5], [1, 3, 4, 6]]
英文:
There is no method named 'unique' for Python lists hence the error.
To identify unique sublists, you can convert the sublists to tuples, create a set of tuples to remove duplicates, and then convert the tuples back to lists. You could solve this as below:
D = [[0, 2, 3, 5], [1, 3, 4, 6], [2, 0, 3, 5], [4, 1, 3, 6]]
#sort the sublist elements (I found this to be able to eliminate them by a set method below) that's if element order doesn't matter in your case
unique_tuples = set(tuple(sorted(sublist)) for sublist in D)
unique_sublists = [list(t) for t in unique_tuples]
print(unique_sublists)
[[0, 2, 3, 5], [1, 3, 4, 6]]
答案5
得分: -1
这在运行。
英文:
This is working.
D = [[0, 2, 3, 5], [1, 3, 4, 6], [2, 0, 3, 5], [4, 1, 3, 6]]
unique_sublists = []
for sublist in D:
if set(sublist) not in [set(x) for x in unique_sublists]:
unique_sublists.append(sublist)
print(unique_sublists)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论