Python中的唯一子列表

huangapple go评论85阅读模式
英文:

Unique sublists in Python

问题

我有一个包含许多子列表的列表D。我想识别唯一的子列表并移除重复的子列表。这里的唯一意味着无论特定元素的位置如何,所有元素都相同。但是我遇到了一个错误。以下是期望的输出。

  1. D = [[0, 2, 3, 5], [1, 3, 4, 6], [2, 0, 3, 5], [4, 1, 3, 6]]
  2. unique_sublists = []
  3. for sublist in D:
  4. if sublist not in unique_sublists:
  5. unique_sublists.append(sublist)
  6. print(unique_sublists)

错误是

  1. in <module>
  2. E=D[i].unique
  3. AttributeError: 'list' object has no attribute 'unique'

期望的输出是

  1. [[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.

  1. D=[[0, 2, 3, 5], [1, 3, 4, 6], [2, 0, 3, 5], [4, 1, 3, 6]]
  2. for i in range(0,len(D)):
  3. E=D[i].unique
  4. print(E)

The error is

  1. in <module>
  2. E=D[i].unique
  3. AttributeError: 'list' object has no attribute 'unique'

The expected output is

  1. [[0, 2, 3, 5], [1, 3, 4, 6]]

答案1

得分: 3

你尚未准确定义“unique”的含义。如果子列表中没有重复项,那么可以执行类似以下操作:

  1. def unique_sublists(data):
  2. seen = set()
  3. result = []
  4. for sub in data:
  5. uniq = frozenset(sub)
  6. if uniq not in seen:
  7. result.append(sub)
  8. seen add(uniq)
  9. return result

如果子列表中可以有重复项,可能需要类似以下操作:

  1. def unique_sublists(data):
  2. from collections import Counter
  3. seen = set()
  4. result = []
  5. for sub in data:
  6. uniq = frozenset(Counter(sub).items())
  7. if uniq not in seen:
  8. result.append(sub)
  9. seen add(uniq)
  10. 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:

  1. def unique_sublists(data):
  2. seen = set()
  3. result = []
  4. for sub in data:
  5. uniq = frozenset(sub)
  6. if uniq not in seen:
  7. result.append(sub)
  8. seen.add(uniq)
  9. return result

If there can be repeats, you might need something like:

  1. def unique_sublists(data):
  2. from collections import Counter
  3. seen = set()
  4. result = []
  5. for sub in data:
  6. uniq = frozenset(Counter(sub).items())
  7. if uniq not in seen:
  8. result.append(sub)
  9. seen.add(uniq)
  10. return result

答案2

得分: 1

你可以使用 set 来获取唯一的项。如果不关心顺序是否改变,你可以使用列表推导,对每个子列表进行排序,将其转换为元组以便进行哈希,然后最终将其转换为集合以去除重复项。

  1. unique = set(tuple(sorted(a)) for a in D)
  2. # 转换回列表
  3. unique = [list(a) for a in unique]

如果你关心顺序,可以做类似的事情,但是创建一个字典以获取原始子列表。

  1. unique = dict(zip([tuple(sorted(a)) for a in D], D)).values()
  2. # 转换回列表
  3. 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.

  1. unique = set(tuple(sorted(a)) for a in D)
  2. # convert back to lists
  3. 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.

  1. unique = dict(zip([tuple(sorted(a)) for a in D], D)).values()
  2. # convert back to lists
  3. unique = [list(a) for a in unique]

答案3

得分: 0

  1. 使用map来排序列表,然后使用set来移除重复的项。

list(set(map(lambda x: tuple(sorted(x)), D)))

  1. 这里使用临时列表,仅在项尚未添加时才添加。
  1. out = []
  2. for i in map(sorted, D):
  3. if i not in out: out.append(i)
英文:

There are multiple ways of doing this, I will add the two I came up with:

  1. Using map to order the lists and then set to remove the duplicated ones.

list(set(map(lambda x: tuple(sorted(x)), D)))

  1. Here using a temporary list to add only if the items have not been added yet.

    1. out = []
    2. for i in map(sorted, D):
    3. if i not in out: out.append(i)

答案4

得分: 0

对于Python列表,没有名为'unique'的方法,因此出现错误。要识别唯一的子列表,你可以将子列表转换为元组,创建一个元组集合来删除重复项,然后将元组转换回列表。你可以按照以下方式解决这个问题:

  1. D = [[0, 2, 3, 5], [1, 3, 4, 6], [2, 0, 3, 5], [4, 1, 3, 6]]
  2. # 对子列表元素进行排序(如果在你的情况下元素顺序不重要)
  3. unique_tuples = set(tuple(sorted(sublist)) for sublist in D)
  4. unique_sublists = [list(t) for t in unique_tuples]
  5. print(unique_sublists)
  6. [[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:

  1. D = [[0, 2, 3, 5], [1, 3, 4, 6], [2, 0, 3, 5], [4, 1, 3, 6]]
  2. #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
  3. unique_tuples = set(tuple(sorted(sublist)) for sublist in D)
  4. unique_sublists = [list(t) for t in unique_tuples]
  5. print(unique_sublists)
  6. [[0, 2, 3, 5], [1, 3, 4, 6]]

答案5

得分: -1

这在运行。

英文:

This is working.

  1. D = [[0, 2, 3, 5], [1, 3, 4, 6], [2, 0, 3, 5], [4, 1, 3, 6]]
  2. unique_sublists = []
  3. for sublist in D:
  4. if set(sublist) not in [set(x) for x in unique_sublists]:
  5. unique_sublists.append(sublist)
  6. print(unique_sublists)

huangapple
  • 本文由 发表于 2023年2月24日 01:54:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75548570.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定