Append values to a set in Python

后端 未结 8 1931
一向
一向 2020-12-12 09:17

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]

相关标签:
8条回答
  • 2020-12-12 10:02
    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.

    0 讨论(0)
  • 2020-12-12 10:08

    Use update like this:

    keep.update(newvalues)
    
    0 讨论(0)
提交回复
热议问题