在 Python 中将值附加到集合

发布于 2024-09-12 12:24:38 字数 30 浏览 2 评论 0原文

如何向现有添加值?

How do I add values to an existing set?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

小清晰的声音 2024-09-19 12:24:38
your_set.update(your_sequence_of_values)

例如,your_set.update([1, 2, 3, 4])。或者,如果您出于某种其他原因必须在循环中生成值,

for value in ...:
    your_set.add(value)

但是,当然,可以使用单个 .update 调用更快、更方便。

your_set.update(your_sequence_of_values)

e.g, your_set.update([1, 2, 3, 4]). Or, if you have to produce the values in a loop for some other reason,

for value in ...:
    your_set.add(value)

But, of course, doing it in bulk with a single .update call is faster and handier, when otherwise feasible.

你是年少的欢喜 2024-09-19 12:24:38

定义一个集合

a = set()

使用 add 到附加单个值

a.add(1)
a.add(2)

使用 update 来从元组、集合、列表或冻结集合中添加元素

a.update([3, 4])
>>> print(a)
{1, 2, 3, 4}

注意:由于集合元素必须是可散列的,并且列表被认为是可变的,因此您不能将列表添加到集合中。您也无法将其他集合添加到集合中。但是,您可以从列表和集合中添加元素,如 .update 方法所示。

Define a set

a = set()

Use add to append single values

a.add(1)
a.add(2)

Use update to add elements from tuples, sets, lists or frozen-sets

a.update([3, 4])
>>> print(a)
{1, 2, 3, 4}

Note: Since set elements must be hashable, and lists are considered mutable, you cannot add a list to a set. You also cannot add other sets to a set. You can however, add the elements from lists and sets as demonstrated with the .update method.

独闯女儿国 2024-09-19 12:24:38

您还可以使用 | 运算符连接两个集合(集合论中的):

>>> my_set = {1}
>>> my_set = my_set | {2}
>>> my_set
{1, 2}

或者使用 |= 的更短形式:

>>> my_set = {1}
>>> my_set |= {2}
>>> my_set
{1, 2}

注意:在Python 2.7之前的版本中,使用set([...])而不是{...}

You can also use the | operator to concatenate two sets (union in set theory):

>>> my_set = {1}
>>> my_set = my_set | {2}
>>> my_set
{1, 2}

Or a shorter form using |=:

>>> my_set = {1}
>>> my_set |= {2}
>>> my_set
{1, 2}

Note: In versions prior to Python 2.7, use set([...]) instead of {...}.

dawn曙光 2024-09-19 12:24:38

像这样使用 update

keep.update(newvalues)

Use update like this:

keep.update(newvalues)
傲影 2024-09-19 12:24:38

这个问题是当人们查找“Python 如何添加元素到集合”时出现在 Google 上的第一个问题,因此值得注意的是,如果要将整个字符串添加到集合中,则应该添加 <代码>.add(),而不是.update()

假设您有一个字符串 foo_str ,其内容为 'this is aeen',并且您有一些等于 set( )

如果你这样做
bar_set.update(foo_str),您的集合的内容将为 {'t', 'a', ' ', 'e', 's', 'n', 'h '、'c'、'i'}

如果您执行 bar_set.add(foo_str),则集合的内容将为 {'this is a Sent'}

This question is the first one that shows up on Google when one looks up "Python how to add elements to set", so it's worth noting explicitly that, if you want to add a whole string to a set, it should be added with .add(), not .update().

Say you have a string foo_str whose contents are 'this is a sentence', and you have some set bar_set equal to set().

If you do
bar_set.update(foo_str), the contents of your set will be {'t', 'a', ' ', 'e', 's', 'n', 'h', 'c', 'i'}.

If you do bar_set.add(foo_str), the contents of your set will be {'this is a sentence'}.

撩动你心 2024-09-19 12:24:38

我只是想在这里添加一个快速注释。所以我正在寻找三种方法中最快的方法。

  1. 使用 set.add() 函数
  2. 使用 set.update() 函数
  3. 使用“|”运算符函数。

我发现要将单个值或多个值添加到集合中,您必须使用 set.add() 函数。这是其他方法中最有效的方法。

所以我运行了一个测试,结果如下:

  1. set.add() Take: 0.5208224999951199
  2. set.update() Take:
    0.6461397000239231`
  3. “|”操作员`拍了:0.7649438999942504

PS:如果你想了解更多分析。

检查此处:附加要设置的值的最快方法

I just wanted to add a quick note here. So I was looking for the fastest method among the three methods.

  1. Using the set.add() function
  2. Using the set.update() function
  3. Using the "|" operator function.

I find it out that to add either a single value or multiple values to a set you have to use the set.add() function. It is the most efficient method among the others.

So I ran a test and Here is the result:

  1. set.add() Took: 0.5208224999951199
  2. set.update() Took:
    0.6461397000239231 `
  3. "|" operator` Took: 0.7649438999942504

PS: If you want to know more the analysis.

Check here : Fastest way to append values to set.

心凉怎暖 2024-09-19 12:24:38

我喜欢这样做的方法是将原始集合和我想要添加到列表中的值都转换,添加它们,然后将它们转换回集合,如下所示:

setMenu = {"Eggs", "Bacon"}
print(setMenu)
> {'Bacon', 'Eggs'}
setMenu = set(list(setMenu) + list({"Spam"}))
print(setMenu)
> {'Bacon', 'Spam', 'Eggs'}
setAdditions = {"Lobster", "Sausage"}
setMenu = set(list(setMenu) + list(setAdditions))
print(setMenu)
> {'Lobster', 'Spam', 'Eggs', 'Sausage', 'Bacon'}

这样我还可以轻松添加多个集合使用相同的逻辑,如果我尝试使用 .update() 方法执行此操作,则会出现 TypeError: unhashable type: 'set' 错误。

The way I like to do this is to convert both the original set and the values I'd like to add into lists, add them, and then convert them back into a set, like this:

setMenu = {"Eggs", "Bacon"}
print(setMenu)
> {'Bacon', 'Eggs'}
setMenu = set(list(setMenu) + list({"Spam"}))
print(setMenu)
> {'Bacon', 'Spam', 'Eggs'}
setAdditions = {"Lobster", "Sausage"}
setMenu = set(list(setMenu) + list(setAdditions))
print(setMenu)
> {'Lobster', 'Spam', 'Eggs', 'Sausage', 'Bacon'}

This way I can also easily add multiple sets using the same logic, which gets me an TypeError: unhashable type: 'set' if I try doing it with the .update() method.

腹黑女流氓 2024-09-19 12:24:38

对我来说,在 Python 3 中,它的工作方式很简单:

keep = keep.union((0,1,2,3,4,5,6,7,8,9,10))

我不知道它是否正确......

For me, in Python 3, it's working simply in this way:

keep = keep.union((0,1,2,3,4,5,6,7,8,9,10))

I don't know if it may be correct...

难得心□动 2024-09-19 12:24:38
keep.update((0,1,2,3,4,5,6,7,8,9,10))

或者

keep.update(np.arange(11))
keep.update((0,1,2,3,4,5,6,7,8,9,10))

Or

keep.update(np.arange(11))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文