在Python中添加以列表推导形式设置的元素

时间:2021-05-21 21:19:33

I have a list of sets. I want to add an element to each of these sets, and I want to do this with list comprehension. This is what I have tried:

我有一套套装。我想为每个集合添加一个元素,我想用list comprehension来做这个。这是我尝试过的:

In [1]: sets1 = [set()]

In [2]: sets2 = [{1,2}, {1,2,3}]

In [3]: [e.add(0) for e in sets1]
Out[3]: [None]

In [4]: [e.add(0) for e in sets2]
Out[4]: [None, None]

My desired output is:

我想要的输出是:

[{0}]
[{1,2,0}, {1,2,3,0}]

Why does the above code return None instead of an addition of elements to the list, and how I can make this work?

为什么上面的代码返回None而不是添加列表中的元素,以及如何使其工作?

4 个解决方案

#1


5  

I would suggest:

我会建议:

[e | {0} for e in sets1]

or:

要么:

[e.union({0}) for e in sets1]

#2


1  

Actually your sets1 and sets2 variables have become the results that you want, because the add statement operates the sets1 but not generate a new list.
You can print(sets1) and print(sets2) to testify.

实际上你的sets1和sets2变量已经成为你想要的结果,因为add语句操作sets1但不生成新的列表。您可以打印(sets1)和打印(sets2)来作证。

#3


1  

Let's first regenerate your problem.

让我们首先重新解决您的问题。

>>> test_set = set()
>>> test_set
set()
>>> print(test_set.add(0))
None
>>> test_set
{0}
>>> 

As you can see, test_set.add(0) returns None. But this is an in place operation, so the item did get added., which is evident from the above snippet.

如您所见,test_set.add(0)返回None。但这是一个就地操作,所以项目确实被添加了。这从上面的片段中可以看出。


How to solve the problem:

如何解决问题:

You can union after making the element a set rather than using the add method.

在将元素设置为集合而不是使用add方法之后,您可以联合。

>>> [i.union({0}) for i in sets2]
[{0, 1, 2}, {0, 1, 2, 3}]

If you have a list/set of element to add to the exiting list of sets, you can do the following:

如果要将一个列表/元素集添加到现有的集合列表中,则可以执行以下操作:

elements_to_add = [3,4,5]
>>> [i.union(set(elements_to_add)) for i in sets2]
[{1, 2, 3, 4, 5}, {1, 2, 3, 4, 5}]

However, this is not an in-place operation. sets2 would be exactly same before and after running the above list comprehension.

但是,这不是就地操作。在运行上面的列表理解之前和之后,sets2将完全相同。

#4


0  

I wouldn't use a list comprehension in this case, a plain for loop would be simpler:

在这种情况下我不会使用列表推导,简单的for循环会更简单:

for subset in sets1:
    subset.add(0)

print(sets1)

should give you the desired output.

应该给你想要的输出。

I already pointed it out in the comments why your approach seemingly did not work:

我已在评论中指出为什么你的方法似乎不起作用:

set.add works in place and does not return anything (thus your Nones). If you want your desired output then run the list-comprehension but don't save its result. Check your set1 and set2 after the list-comprehension to get the desired output.

set.add在原地工作,不返回任何东西(因此你的Nones)。如果您需要所需的输出,请运行list-comprehension但不保存其结果。在list-comprehension之后检查set1和set2以获得所需的输出。

So you could just check sets1 and sets2 after the list comprehension. It should return: [{0}] and [{1,2,0}, {1,2,3,0}] (order may vary because sets are unordered).

所以你可以在列表理解之后检查sets1和sets2。它应该返回:[{0}]和[{1,2,0},{1,2,3,0}](顺序可能会有所不同,因为集合是无序的)。

#1


5  

I would suggest:

我会建议:

[e | {0} for e in sets1]

or:

要么:

[e.union({0}) for e in sets1]

#2


1  

Actually your sets1 and sets2 variables have become the results that you want, because the add statement operates the sets1 but not generate a new list.
You can print(sets1) and print(sets2) to testify.

实际上你的sets1和sets2变量已经成为你想要的结果,因为add语句操作sets1但不生成新的列表。您可以打印(sets1)和打印(sets2)来作证。

#3


1  

Let's first regenerate your problem.

让我们首先重新解决您的问题。

>>> test_set = set()
>>> test_set
set()
>>> print(test_set.add(0))
None
>>> test_set
{0}
>>> 

As you can see, test_set.add(0) returns None. But this is an in place operation, so the item did get added., which is evident from the above snippet.

如您所见,test_set.add(0)返回None。但这是一个就地操作,所以项目确实被添加了。这从上面的片段中可以看出。


How to solve the problem:

如何解决问题:

You can union after making the element a set rather than using the add method.

在将元素设置为集合而不是使用add方法之后,您可以联合。

>>> [i.union({0}) for i in sets2]
[{0, 1, 2}, {0, 1, 2, 3}]

If you have a list/set of element to add to the exiting list of sets, you can do the following:

如果要将一个列表/元素集添加到现有的集合列表中,则可以执行以下操作:

elements_to_add = [3,4,5]
>>> [i.union(set(elements_to_add)) for i in sets2]
[{1, 2, 3, 4, 5}, {1, 2, 3, 4, 5}]

However, this is not an in-place operation. sets2 would be exactly same before and after running the above list comprehension.

但是,这不是就地操作。在运行上面的列表理解之前和之后,sets2将完全相同。

#4


0  

I wouldn't use a list comprehension in this case, a plain for loop would be simpler:

在这种情况下我不会使用列表推导,简单的for循环会更简单:

for subset in sets1:
    subset.add(0)

print(sets1)

should give you the desired output.

应该给你想要的输出。

I already pointed it out in the comments why your approach seemingly did not work:

我已在评论中指出为什么你的方法似乎不起作用:

set.add works in place and does not return anything (thus your Nones). If you want your desired output then run the list-comprehension but don't save its result. Check your set1 and set2 after the list-comprehension to get the desired output.

set.add在原地工作,不返回任何东西(因此你的Nones)。如果您需要所需的输出,请运行list-comprehension但不保存其结果。在list-comprehension之后检查set1和set2以获得所需的输出。

So you could just check sets1 and sets2 after the list comprehension. It should return: [{0}] and [{1,2,0}, {1,2,3,0}] (order may vary because sets are unordered).

所以你可以在列表理解之后检查sets1和sets2。它应该返回:[{0}]和[{1,2,0},{1,2,3,0}](顺序可能会有所不同,因为集合是无序的)。