在 Python 中将值附加到集合
如何向现有集
添加值?
How do I add values to an existing set
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如何向现有集
添加值?
How do I add values to an existing set
?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(9)
例如,
your_set.update([1, 2, 3, 4])
。或者,如果您出于某种其他原因必须在循环中生成值,但是,当然,可以使用单个
.update
调用更快、更方便。e.g,
your_set.update([1, 2, 3, 4])
. Or, if you have to produce the values in a loop for some other reason,But, of course, doing it in bulk with a single
.update
call is faster and handier, when otherwise feasible.定义一个集合
使用
add
到附加单个值使用
update
来从元组、集合、列表或冻结集合中添加元素注意:由于集合元素必须是可散列的,并且列表被认为是可变的,因此您不能将列表
添加
到集合中。您也无法将其他集合添加
到集合中。但是,您可以从列表和集合中添加元素,如.update
方法所示。Define a set
Use
add
to append single valuesUse
update
to add elements from tuples, sets, lists or frozen-setsNote: Since set elements must be hashable, and lists are considered mutable, you cannot
add
a list to a set. You also cannotadd
other sets to a set. You can however, add the elements from lists and sets as demonstrated with the.update
method.您还可以使用
|
运算符连接两个集合(集合论中的并):或者使用
|=
的更短形式:注意:在Python 2.7之前的版本中,使用
set([...])
而不是{...}
。You can also use the
|
operator to concatenate two sets (union in set theory):Or a shorter form using
|=
:Note: In versions prior to Python 2.7, use
set([...])
instead of{...}
.像这样使用
update
:Use
update
like this:这个问题是当人们查找“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 setbar_set
equal toset()
.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'}
.我只是想在这里添加一个快速注释。所以我正在寻找三种方法中最快的方法。
我发现要将单个值或多个值添加到集合中,您必须使用 set.add() 函数。这是其他方法中最有效的方法。
所以我运行了一个测试,结果如下:
set.add()
Take: 0.5208224999951199set.update()
Take:0.6461397000239231`
PS:如果你想了解更多分析。
检查此处:附加要设置的值的最快方法。
I just wanted to add a quick note here. So I was looking for the fastest method among the three methods.
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:
set.add()
Took: 0.5208224999951199set.update()
Took:0.6461397000239231 `
PS: If you want to know more the analysis.
Check here : Fastest way to append values to set.
我喜欢这样做的方法是将原始集合和我想要添加到列表中的值都转换,添加它们,然后将它们转换回集合,如下所示:
这样我还可以轻松添加多个集合使用相同的逻辑,如果我尝试使用
.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:
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.对我来说,在 Python 3 中,它的工作方式很简单:
我不知道它是否正确......
For me, in Python 3, it's working simply in this way:
I don't know if it may be correct...
或者
Or