[英]Finding unique elements in nested list
If I have a list mylist = ["[amn,b,c]", "['a,d,e']", "['f,b,e']"] , and I need to a single list with all the unique elements as [amn,b,c,d,e,f] , how can I accomplish that? 如果我有一个列表mylist = [“ [amn,b,c]”,“ ['a,d,e']”,“ ['f,b,e']”]] ,并且我需要一个列表使用[amn,b,c,d,e,f]的所有唯一元素,我该如何实现?
I have tried creating a function and also tried some other method, but to no avail. 我尝试过创建一个函数,也尝试过其他方法,但无济于事。
Function: 功能:
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)
Output: 输出:
[amn,b,c]
[‘a,d,e’]
[‘f,b,e’]
Other method: 其他方法:
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
Output: 输出:
['a', 'b', 'c', 'd', 'e', 'f', 'm', 'n']
Expected Results: 预期成绩:
[amn,b,c,d,e,f]
Actual Results: 实际结果:
With the function: 具有的功能:
[amn,b,c]
[‘a,d,e’]
[‘f,b,e’]
With the other method: 用另一种方法:
['a', 'b', 'c', 'd', 'e', 'f', 'm', 'n']
You could use the following list comprehension, where re.sub
is used in order to remove unwanted characters, and the underlying lists are obtained using .split
, and splitting by ,
. 您可以使用以下列表
re.sub
方法,其中使用re.sub
来删除不需要的字符,并且使用.split
并用,
拆分来获取基础列表。
Finally in order to obtain the unique elements from the nested list you can use itertools.chain
to flatten the nested list, and generate a set
from the result in order to keep unique values: 最后,为了从嵌套列表中获取唯一元素,可以使用
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'}
Where: 哪里:
[re.sub(r'[\[‘’\"\]]', '', i).split(',') for i in mylist]
[['amn', 'b', 'c'], ['a', 'd', 'e'], ['f', 'b', 'e']]
I had to redefine the list differently because before you had 1 list with strings. 我必须重新定义列表,因为在您有1个带有字符串的列表之前。 If this is wrong let me know, however I am curious as to why you have a list of list-like strings.
如果这是错误的,请让我知道,但是我很好奇您为什么要列出类似列表的字符串。
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)
result: 结果:
['amn', 'b', 'c', 'a', 'd', 'e', 'f']
[Finished in 0.081s]
or if you need to keep your nested lists within quotations you can use this: 或者,如果您需要将嵌套列表保留在引号内,则可以使用以下方法:
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)
to test this I added a stringed list "['r','t','x']"
and this should catch r
, t
, x
as unique inputs 为了测试这一点,我添加了一个字符串列表
"['r','t','x']"
,它应该捕获r
, t
, x
作为唯一输入
and this results: 结果:
['amn', 'b', 'c', 'r', 't', 'x', 'a', 'd', 'e', 'f']
[Finished in 0.077s]
This will work whether it's a list of lists and stringed lists and all since the function is recursive. 无论函数是列表列表还是字符串列表,都可以使用,因为该函数是递归的。
Firstly, I would try to substitute the ,
(comma), '
(single quote), []
(open close square brackets with empty string using pattern matching. Then remove duplicates using set
and reconstruct the list using list
as below: 首先,我将尝试用
,
(逗号), '
(单引号), []
(使用模式匹配用空字符串打开封闭的方括号。然后使用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)
where 哪里
re.sub(',|\'|\[|]|\s+', '', word)])
will replace special characters in the string. 将替换字符串中的特殊字符。 For example,
['a, d,e']
to ade
. 例如,
['a, d,e']
到ade
。
The comprehension based solution is technically equal to 基于理解的解决方案在技术上等于
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)
which results as 结果是
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.