1

我有一串词:

foo = "This is a string"

我还有一个按以下方式格式化的列表:

bar = ["this","3"], ["is","5"]

我需要制作一个脚本,在 foo 中搜索 bar 中的单词,如果找到一个单词,计数器应该在 bar 中的单词旁边添加数字。我已经走到这一步了:

bar_count=0
for a,b in foo:
   if bar in a:
       bar_count+=b

但这似乎不起作用,有人知道吗?

4

6 回答 6

2

使用字典记数;

foo = "This is a string"
words = foo.split()
count = {}
scores = {"this": 3,
          "is": 5
}

for word in words:
    if word not in count:
        count[word] = 0

    if word in scores:
        count[word] += scores[word]
    else:
        count[word] += 1
于 2013-07-15T13:24:11.983 回答
1

这应该适合你的情况

foo = "This is a string"
bar = ["this","3"], ["is","5"]

bar_count = 0
for word, value in bar:
   if foo.count(word) > 0:
       bar_count += int(value)
于 2013-07-15T13:31:48.480 回答
1

使用collections.defaultdict

>>> foo = "This is a string string This bar"
>>> dic = collections.defaultdict(int)
>>> for f in foo.split():
...     dic[f] += 1
>>> dic
defaultdict(<type 'int'>, {'This': 2, 'a': 1, 'is': 1, 'bar': 1, 'string': 2})

编辑

从您当前拥有的列表中创建一个字典,字典是数据的更好表示

>>> foo = 'this is a string this bar'
>>> bar = [['this', 3], ['is', 5]]
>>> dic = dict(bar)
>>> dict(bar)
{'this': 3, 'is': 5}

现在,在您的字符串中查找单词并添加内容

>>> for f in foo.split():
...     try:
...         dic[f] += 1
...     except:
...         pass
>>> dic
{'this': 5, 'is': 6}

这有帮助吗?

于 2013-07-15T13:26:48.407 回答
1

此代码将创建一个字典,其中找到的单词作为键,值将是单词出现的次数:

foo = "This is a string is is"
bar = {}

words = foo.split(" ")

for w in words:
    if(w in bar):
        # its there, just increment its value
        bar[w] += 1
    else:
        # its not yet there, make new key with value 1
        bar[w] = 1

for i in bar:
    print i,"->", bar[i]

此代码产生:

>>> 
This -> 1
a -> 1
is -> 3
string -> 1
于 2013-07-15T13:30:28.447 回答
1

这不使用显式循环(除了理解),而且我认为很容易理解:

import collections
weight_list = ["this","3"], ["is","5"]
foo = "This is a string"

def weighted_counter(weight_list, countstring):
    #create dict {word:count of word}. uses lower() because that's
    # the format of the weight_list
    counts = collections.Counter(countstring.lower().split())

    #multiply weight_list entries by the number of appearances in the string
    return {word:int(weight)*counts.get(word,0) for word,weight in weight_list}

print weighted_counter(weight_list, foo)
#{'this': 3, 'is': 5}
#take the sum of the values (not keys) in the dict returned
print sum(weighted_counter(weight_list, "that is the this is it").itervalues())
#13

在行动:http: //ideone.com/ksdI1b

于 2013-07-15T13:40:57.440 回答
1

如果您只想要一个总数 - 转换bar为 adict并使用它来查找有效单词,并默认为 unknown0来运行它sum

foo = "This is a string"
bar = ["this","3"], ["is","5"]
scores = {w: int(n) for w, n in bar}
bar_count = sum(scores.get(word, 0) for word in foo.lower().split())
# 8

如果您想要单词数,但从以下指定的总数开始bar

from collections import Counter
start = Counter({w: int(n) for w, n in bar})
total = start + Counter(foo.lower().split())
# Counter({'is': 6, 'this': 4, 'a': 1, 'string': 1})
于 2013-07-15T13:31:03.587 回答