从 python 嵌套列表中以任意顺序删除重复字符串
Removing duplicate strings from python nested list present in any order
[["hello", "bye", "start"], ["bye", "start", "hello"], ["john", "riya", "tom"], ["riya","john", "tom"].....]
我有一个这样的列表。我想从 Python 中的嵌套列表中删除重复元素,其中元素应按任何顺序排列。
输出应该是:-
[["hello", "bye", "start"], ["john", "riya", "tom"]]
3 个字符串在任何列表中只能出现一次。
如何实现?
一个简单而天真的解决方案
arr = [["hello", "bye", "start"], ["bye", "start", "hello"], ["john", "riya", "tom"], ["riya","john", "tom"]]
seen, tmp, result = list()
for i in arr:
for j in i:
if j not in seen:
seen.append(j)
tmp.append(j)
if len(tmp) == 3:
result.append(tmp)
tmp = list()
print result
请记住,这只会附加包含 3 个元素的列表,因此某些情况下可能会导致输出列表中缺少元素(根据您的示例和给出的说明)
我认为这是一个非常简单的解决方案:
a = [['hello', 'bye', 'start'], ['bye', 'start', 'hello'], ['john', 'riya', 'tom'], ['riya', 'john', 'tom']]
for i in range(len(a)):
a[i].sort()
for i in a:
if i not in b:
b.append(i)
它输出:
[['bye', 'hello', 'start'], ['john', 'riya', 'tom']]
试试这个:-
a = [["hello", "bye", "start"], ["bye", "start", "hello"], ["john", "riya", "tom"], ["riya","john", "tom"]]
ls = []
for i in a:
i = sorted(i)
if i not in ls:
ls.append(i)
print(ls)
正如你所说,想要像列表一样的输出然后尝试这个棘手的方法但是它不会是 pythonic 方式:-
ls = []
ind = []
for i,j in enumerate(a):
j = sorted(j)
if j not in ls:
ind.append(i)
ls.append(j)
ls1 = [a[x] for x in ind]
print(ls1)
输出:-
[['hello', 'bye', 'start'], ['john', 'riya', 'tom']]
只需转换成 set
它会自动删除项目。
a = [list(i) for i in {frozenset(k) for k in a}]
您可以使用 enumerate
:
s = [["hello", "bye", "start"], ["bye", "start", "hello"], ["john", "riya", "tom"], ["riya","john", "tom"]]
new_s = [a for i, a in enumerate(s) if not any(all(c in h for c in a) for h in s[:i])]
输出:
[['hello', 'bye', 'start'], ['john', 'riya', 'tom']]
[["hello", "bye", "start"], ["bye", "start", "hello"], ["john", "riya", "tom"], ["riya","john", "tom"].....]
我有一个这样的列表。我想从 Python 中的嵌套列表中删除重复元素,其中元素应按任何顺序排列。
输出应该是:-
[["hello", "bye", "start"], ["john", "riya", "tom"]]
3 个字符串在任何列表中只能出现一次。 如何实现?
一个简单而天真的解决方案
arr = [["hello", "bye", "start"], ["bye", "start", "hello"], ["john", "riya", "tom"], ["riya","john", "tom"]]
seen, tmp, result = list()
for i in arr:
for j in i:
if j not in seen:
seen.append(j)
tmp.append(j)
if len(tmp) == 3:
result.append(tmp)
tmp = list()
print result
请记住,这只会附加包含 3 个元素的列表,因此某些情况下可能会导致输出列表中缺少元素(根据您的示例和给出的说明)
我认为这是一个非常简单的解决方案:
a = [['hello', 'bye', 'start'], ['bye', 'start', 'hello'], ['john', 'riya', 'tom'], ['riya', 'john', 'tom']]
for i in range(len(a)):
a[i].sort()
for i in a:
if i not in b:
b.append(i)
它输出:
[['bye', 'hello', 'start'], ['john', 'riya', 'tom']]
试试这个:-
a = [["hello", "bye", "start"], ["bye", "start", "hello"], ["john", "riya", "tom"], ["riya","john", "tom"]]
ls = []
for i in a:
i = sorted(i)
if i not in ls:
ls.append(i)
print(ls)
正如你所说,想要像列表一样的输出然后尝试这个棘手的方法但是它不会是 pythonic 方式:-
ls = []
ind = []
for i,j in enumerate(a):
j = sorted(j)
if j not in ls:
ind.append(i)
ls.append(j)
ls1 = [a[x] for x in ind]
print(ls1)
输出:-
[['hello', 'bye', 'start'], ['john', 'riya', 'tom']]
只需转换成 set
它会自动删除项目。
a = [list(i) for i in {frozenset(k) for k in a}]
您可以使用 enumerate
:
s = [["hello", "bye", "start"], ["bye", "start", "hello"], ["john", "riya", "tom"], ["riya","john", "tom"]]
new_s = [a for i, a in enumerate(s) if not any(all(c in h for c in a) for h in s[:i])]
输出:
[['hello', 'bye', 'start'], ['john', 'riya', 'tom']]