简体   繁体   English

分开的字符串列表

[英]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) (假设您想将单词作为集合的元素)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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