将值附加到 Python 中的集合 [英] Append values to a set in Python

查看:27
本文介绍了将值附加到 Python 中的集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一套这样的:

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

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

How do I add values [0,1,2,3,4,5,6,7,8,9,10] into this set?

推荐答案

keep.update(yoursequenceofvalues)

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

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)

但是,当然,在其他可行的情况下,使用单个 .update 调用批量完成它会更快更方便.

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

这篇关于将值附加到 Python 中的集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆