[英]Finding unique elements in nested list
如果我有一个列表mylist = [“ [amn,b,c]”,“ ['a,d,e']”,“ ['f,b,e']”]] ,并且我需要一个列表使用[amn,b,c,d,e,f]的所有唯一元素,我该如何实现?
我尝试过创建一个函数,也尝试过其他方法,但无济于事。
功能:
mylist = ["[amn,b,c]", "[‘a,d,e’]", "[‘f,b,e’]"]
def print_list(the_list):
for each_item in the_list:
if isinstance(each_item, list):
print_list(each_item)
else:
print(each_item)
print_list(mylist)
输出:
[amn,b,c]
[‘a,d,e’]
[‘f,b,e’]
其他方法:
mylist = ["[amn,b,c]", "[‘a, d,e’]", "[‘f,b,e’]"]
mylist = str(mylist)
mylist = str(mylist)
mylist = [str(x) for x in (mylist)]
mylist = set(mylist)
i = {' ', "'", ',', '[', ']','‘', '’'}
mylist.difference_update(i)
mylist = list(mylist)
mylist.sort()
mylist
输出:
['a', 'b', 'c', 'd', 'e', 'f', 'm', 'n']
预期成绩:
[amn,b,c,d,e,f]
实际结果:
具有的功能:
[amn,b,c]
[‘a,d,e’]
[‘f,b,e’]
用另一种方法:
['a', 'b', 'c', 'd', 'e', 'f', 'm', 'n']
您可以使用以下列表re.sub
方法,其中使用re.sub
来删除不需要的字符,并且使用.split
并用,
拆分来获取基础列表。
最后,为了从嵌套列表中获取唯一元素,可以使用itertools.chain
展平嵌套列表,并从结果生成一个set
以保留唯一值:
import re
from itertools import chain
set(chain(*[re.sub(r'[\[‘’\"\]]', '', i).split(',') for i in mylist]))
{'a', 'amn', 'b', 'c', 'd', 'e', 'f'}
哪里:
[re.sub(r'[\[‘’\"\]]', '', i).split(',') for i in mylist]
[['amn', 'b', 'c'], ['a', 'd', 'e'], ['f', 'b', 'e']]
我必须重新定义列表,因为在您有1个带有字符串的列表之前。 如果这是错误的,请让我知道,但是我很好奇您为什么要列出类似列表的字符串。
mylist = [['amn','b','c'], ['a','d','e'], ['f','b','e']]
unique_list = []
def find_all_unique(input, unique_list):
if type(input) is list:
return [find_all_unique(x, unique_list) for x in input if x is not None]
if type(input) is str:
if input not in unique_list:
unique_list.append(input)
find_all_unique(mylist, unique_list)
print(unique_list)
结果:
['amn', 'b', 'c', 'a', 'd', 'e', 'f']
[Finished in 0.081s]
或者,如果您需要将嵌套列表保留在引号内,则可以使用以下方法:
mylist = [['amn','b','c', "['r','t','x']"], ['a','d','e'], ['f','b','e']]
unique_list = []
def find_all_unique(input, unique_list):
if type(input) is list:
return [find_all_unique(x, unique_list) for x in input if x is not None]
if type(input) is str:
if input.startswith('['):
temp_list=[]
exec("temp_list.append(" + input + ')', {"temp_list":temp_list})
return [find_all_unique(x, unique_list) for x in temp_list if x is not None]
elif input not in unique_list:
unique_list.append(input)
find_all_unique(mylist, unique_list)
print(unique_list)
为了测试这一点,我添加了一个字符串列表"['r','t','x']"
,它应该捕获r
, t
, x
作为唯一输入
结果:
['amn', 'b', 'c', 'r', 't', 'x', 'a', 'd', 'e', 'f']
[Finished in 0.077s]
无论函数是列表列表还是字符串列表,都可以使用,因为该函数是递归的。
首先,我将尝试用,
(逗号), '
(单引号), []
(使用模式匹配用空字符串打开封闭的方括号。然后使用set
删除重复项,并使用list
重建list
,如下所示:
my_list = ["[amn,b,c]", "['a, d,e']", "['f,b,e']"]
result = sorted(list(set(([letter for word in my_list for letter in re.sub(',|\'|\[|]|\s+', '', word)]))))
print(result)
哪里
re.sub(',|\'|\[|]|\s+', '', word)])
将替换字符串中的特殊字符。 例如, ['a, d,e']
到ade
。
基于理解的解决方案在技术上等于
result = []
for word in my_list: # Break list of lists to lists
word = re.sub(',|\'|\[|]|\s+', '', word)
for letter in word: # Process each word in the sub list
result.append(letter)
print('results with duplicates: ', result) # List with possible duplicates
result = set(result) # Remove duplicates by converting to a set
result = list(result) # Convert set back to list without duplicates (order is not preserved)
print('results without duplicates: ', result)
result = sorted(result)
print('results in sorted order: ', result)
结果是
results with duplicates: ['a', 'm', 'n', 'b', 'c', 'a', 'd', 'e', 'f', 'b', 'e']
results without duplicates: ['e', 'a', 'd', 'm', 'f', 'c', 'n', 'b']
results in sorted order: ['a', 'b', 'c', 'd', 'e', 'f', 'm', 'n']
dd = []
mylist = ["[amn,b,c]", "[‘a,d,e’]", "[‘f,b,e’]"]
for i in mylist:
dd.extend([''.join(filter(str.isalnum, j)) for j in i.split(",")])
print (list(set(dd)))
#output ['f', 'a', 'b', 'amn', 'c', 'd', 'e']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.