将值附加到 Python 中的集合
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3392354/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Append values to a set in Python
提问by l--''''''---------''''''''''''
I have a set like this:
我有一套这样的:
keep = set(generic_drugs_mapping[drug] for drug in drug_input)
How do I add values [0,1,2,3,4,5,6,7,8,9,10]into this set?
如何将值添加[0,1,2,3,4,5,6,7,8,9,10]到该集合中?
采纳答案by Alex Martelli
keep.update(yoursequenceofvalues)
e.g, keep.update(xrange(11))for your specific example. Or, if you haveto produce the values in a loop for some other reason,
例如,keep.update(xrange(11))对于您的具体示例。或者,如果由于其他原因必须在循环中生成值,
for ...whatever...:
onemorevalue = ...whatever...
keep.add(onemorevalue)
But, of course, doing it in bulk with a single .updatecall is faster and handier, when otherwise feasible.
但是,当然.update,在其他可行的情况下,通过单个调用批量完成它会更快更方便。
回答by sberry
Use updatelike this:
update像这样使用:
keep.update(newvalues)
回答by nyuszika7h
You can also use the |operator to concatenate two sets (unionin set theory):
您还可以使用|运算符连接两个集合(集合论中的union):
>>> 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 {...}.
注意:在 Python 2.7 之前的版本中,使用set([...])代替{...}.
回答by RandallShanePhD
Define set
定义集
a = set()
Use add to append single values
使用 add 附加单个值
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}
If you want to add a tuple or frozen-set itself, use add
如果要添加元组或冻结集本身,请使用 add
a.add((5, 6))
>> print(a)
{1, 2, 3, 4, (5, 6)}
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.
注意:由于集合元素必须是可散列的,并且列表被认为是可变的,因此您不能将列表添加到集合中。您也不能将其他集合添加到集合中。但是,您可以像使用“.update”方法演示的那样从列表和集合中添加元素。
回答by lalilulelost
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().
这个问题是第一个出现在谷歌上的问题,当你查找“Python如何向集合添加元素”时,所以值得注意的是,如果你想将整个字符串添加到集合中,应该添加.add(),不是.update()。
Say you have a string foo_strwhose contents are 'this is a sentence', and you have some set bar_setequal to set().
假设您有一个字符串,foo_str其内容为'this is a sentence',并且您有一些bar_set等于set().
If you do
bar_set.update(foo_str), the contents of your set will be {'t', 'a', ' ', 'e', 's', 'n', 'h', 'c', 'i'}.
如果你这样做
bar_set.update(foo_str),你的集合的内容将是{'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'}.
如果你这样做bar_set.add(foo_str),你的集合的内容将是{'this is a sentence'}。
回答by decadenza
For me, in Python 3, it's working simply in this way:
对我来说,在 Python 3 中,它的工作方式很简单:
keep = keep.union((0,1,2,3,4,5,6,7,8,9,10))
I don't know if it may be correct...
不知道说得对不对...
回答by rishi jain
keep.update((0,1,2,3,4,5,6,7,8,9,10))
Or
或者
keep.update(np.arange(11))
回答by Kira Resari
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.
通过这种方式,我还可以使用相同的逻辑轻松添加多个集合,TypeError: unhashable type: 'set'如果我尝试使用该.update()方法进行操作,就会得到一个结果。

