fork download
  1. import collections
  2. weight_list = ["this","3"], ["is","5"]
  3. foo = "This is a string"
  4.  
  5. def weighted_counter(weight_list, countstring):
  6. counts = collections.Counter(countstring.lower().split())
  7. return {word:int(weight)*counts.get(word,0) for word,weight in weight_list}
  8.  
  9. print weighted_counter(weight_list, foo)
  10. print sum(weighted_counter(weight_list, "that is the this is it").itervalues())
Success #stdin #stdout 0.01s 7856KB
stdin
Standard input is empty
stdout
{'this': 3, 'is': 5}
13