简体   繁体   中英

List of Strings to separate sets

So I am having problems with a problem. I have to write a function that turns a list of strings( I don't know how many strings will be in the list as there are different test cases with different lists) into separate sets. I have to do this because I want to use the symmetric difference property of sets on the produced sets later on. For instance how would I convert this list of strings into four separate sets?

(["zebra bear fox elephant", "bear crocodile fox", "rhino elephant crocodile kangaroo", "elephant bear"])

Please remember that I will not know the number of strings in each list and must find a general solution that holds for all.

Straightforward list comprehension:

[set(x.split()) for x in my_list]

(assuming you want words as the elements of your sets)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM