简体   繁体   English

如何在python中将一个包含5个元素的集合变成5个集合,每个集合一个元素?

[英]how to turn one set with 5 elements into 5 sets each with one element in python?

Lets say I have 2 sets of lists.假设我有 2 组列表。

set1 = [2, 4, 6, 8, 10]
set2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

From here I've tried to subtract set1 from set2 in order to get [1, 3, 5, 7, 9] , and I've done so by saying print [x for x in set2 if x not in set1] The output is just set2 , because there is only one element in set 2, and that is the list of all natural numbers up to and including 10.从这里开始,我尝试从 set2 中减去 set1 以获得[1, 3, 5, 7, 9] ,我这样做是通过说 print [x for x in set2 if x not in set1]输出只是set2 ,因为在集合 2 中只有一个元素,那就是所有自然数的列表,包括 10。

So my question is how do I turn [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] into [[1],[2],[3],[4],[5],[6],[7],[8],[9],[10]] ?所以我的问题是如何将[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]变成[[1],[2],[3],[4],[5],[6],[7],[8],[9],[10]] ?

I know this is a basic question, but I'm new and I couldn't find any other questions like this.我知道这是一个基本问题,但我是新手,找不到任何其他类似的问题。 Please answer with answers that help in general and not only in this situation.请回答对一般情况有帮助的答案,而不仅仅是在这种情况下。 Thanks in advance.提前致谢。

You can try this:你可以试试这个:

s = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
new_s = [[i] for i in s]

Output:输出:

[[1], [2], [3], [4], [5], [6], [7], [8], [9], [10]]

you can use sets to solve the stated problem您可以使用集合来解决所述问题

set1 = [2, 4, 6, 8, 10]
set2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

set(set2) - set(set1)

Out[18]: {1, 3, 5, 7, 9}  

the list comprehension works too, did you have a different result?列表理解也有效,你有不同的结果吗?

[e for e in set2 if e not in set1]

Out[17]: [1, 3, 5, 7, 9]

暂无
暂无

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

相关问题 如何创建一组仅包含在多个集合中的所有元素的新集合? - How to create a new set of all elements that are in only one of multiple sets? Python 如何从组中获取元素的所有组合,每个组中最多有一个元素,并且其中一个组中至少有一个元素 - Python how to get all combinations of elements from groups with at most one element from each group and at least one element from one of the groups 来自多个集合的组合,其中每个结果只有每个集合中的一个元素 - Combinations from multiple sets where each result has only one element from each set 检查集合/列表中至少一个元素是否在列表/集合集合中的每个元素中的最快方法 - fastest way to check if atleast one element in set/list is in each element in a collection of lists/sets Python:如何获取仅出现在一组列表中的项目? - Python: How to get items that appear in only one set of a list of sets? 在python中,如何通过条件对一维数组的对应元素快速设置二维数组的每一行的值? - In python, how to quickly set the value of each row of a 2 dimensional array by condition on the corresponding element of a one dimensional array? Python遍历两个集合以比较每个集合的所有元素 - Python looping through two sets to compare all elements of each set "在 Python 中将 ONE 元素字典转换为 ONE 元组的快捷方式" - Shortcut to turn a ONE element dict into ONE tuple in Python 如何将列表元素一分为二? - How to turn one list element in two? 当总共有一百万个字段时,如何比较给定的集合和可用的集合以找到具有最大交集元素的集合? - How to compare given set with available sets to find the one with most intersecting elements, when there are a million fields in total?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM