将值附加到Python中的集合 [英] Append values to a set in Python
本文介绍了将值附加到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屋!
查看全文