Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
381 views
in Technique[技术] by (71.8m points)

Get unique values in List of Lists in python

I want to create a list (or set) of all unique values appearing in a list of lists in python. I have something like this:

aList=[['a','b'], ['a', 'b','c'], ['a']]

and i would like the following:

unique_values=['a','b','c']

I know that for a list of strings you can just use set(aList), but I can't figure how to solve this in a list of lists, since set(aList) gets me the error message

unhashable type: 'list'

How can i solve it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
array = [['a','b'], ['a', 'b','c'], ['a']]
result = {x for l in array for x in l}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...