问题:将值附加到Python中的集合

我有这样的一套:

keep = set(generic_drugs_mapping[drug] for drug in drug_input)

如何将值添加[0,1,2,3,4,5,6,7,8,9,10]到此集中?

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

keep.update(yoursequenceofvalues)

例如,keep.update(xrange(11))对于您的特定示例。或者,如果由于某些其他原因必须在循环中生成值,

for ...whatever...:
  onemorevalue = ...whatever...
  keep.add(onemorevalue)

但是,当然.update,在其他可行的情况下,只需一次调用即可批量执行此操作更快,更方便。

keep.update(yoursequenceofvalues)

e.g, keep.update(xrange(11)) for your specific example. Or, if you have to produce the values in a loop for some other reason,

for ...whatever...:
  onemorevalue = ...whatever...
  keep.add(onemorevalue)

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


回答 1

定义集合

a = set()

使用添加附加单个值

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

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

a.update([3,4])

>> print(a)
{1, 2, 3, 4}

如果要添加元组或冻结集本身,请使用add

a.add((5, 6))

>> print(a)
{1, 2, 3, 4, (5, 6)}

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

Define 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}

If you want to add a tuple or frozen-set itself, use 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.


回答 2

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

>>> 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 {...}.


回答 3

update像这样使用:

keep.update(newvalues)

Use update like this:

keep.update(newvalues)

回答 4

这个问题是第一个在Google查询“ Python如何添加要设置的元素”时显示在Google上的问题,因此,值得注意的是,如果您想将整个字符串添加到集合中,则应添加.add(),不.update()

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

如果这样做 bar_set.update(foo_str),您设置的内容将是{'t', 'a', ' ', 'e', 's', 'n', 'h', 'c', 'i'}

如果这样做bar_set.add(foo_str),您设置的内容将是{'this is a sentence'}

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'}.


回答 5

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

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'}

这样,我也可以使用相同的逻辑轻松添加多个集合,TypeError: unhashable type: 'set'如果尝试使用该.update()方法,则会得到一个提示。

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.


回答 6

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))

回答 7

对我来说,在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…


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。