Python 3.x 计算一个字符串中的每个字符串

Python 3.x 计算一个字符串中的每个字符串,python-3.x,Python 3.x,我正在发短信 text=''让自己在当下快乐的最好方法之一是 回忆过去的快乐时光“ 我想匹配匹配列表中的每个单词并计算它们的出现次数 match=[‘祝我生日快乐’、‘约翰给我发了备忘录应用程序’、‘自助时间’、‘呼叫rom’] 示例-在第一次迭代中,即祝我生日快乐 快乐被计算2次, 生日计数为0次, 到计数为2次, me被计数1次。(memories中的“me”) 我希望结果是- happy(2) birthday(0) to(2) me(1) john(0) sent(1) me(1) t

我正在发短信

text=''让自己在当下快乐的最好方法之一是
回忆过去的快乐时光“
我想匹配匹配列表中的每个单词并计算它们的出现次数

match=[‘祝我生日快乐’、‘约翰给我发了备忘录应用程序’、‘自助时间’、‘呼叫rom’]

示例-在第一次迭代中,即祝我生日快乐

快乐被计算2次, 生日计数为0次, 计数为2次, me被计数1次。(memories中的“me”)

我希望结果是-

happy(2) birthday(0) to(2) me(1)
john(0) sent(1) me(1) the(3) memo(1) app(2)
self(1) time(1)
call(1) the(3) rom(1)
我试过这个

for textmatch in match:
    num=text.count(textmatch)
    textmatch= textmatch +'(' + str(num) +')'
    print(textmatch)

但是这不起作用。

您需要迭代
匹配字符串中的每个单词。这可以通过拆分(在空间上)来实现

另一个妙招是打印出每个单词,并将可选的
end
参数设置为一个空格(
'
)。这意味着它们都停留在一行上,直到发出一个普通的
print()
调用并移动到下一行

for textmatch in match:
    for word in textmatch.split():
        num=text.count(word)
        print(word + '(' + str(num) + ')', end=' ')
    print()
在解释器中测试:

>>> text= ''' One of the best ways to make yourself happy in the present is to 
...            recall happy times from the past memories '''
>>> 
>>> match=['happy birthday to me' , 'john sent me the memo app' , 'self time' , 'call the rom']
>>> for textmatch in match:
...     for word in textmatch.split():
...         num=text.count(word)
...         print(word + '(' + str(num) + ')', end=' ')
...     print()
... 
happy(2) birthday(0) to(2) me(2) 
john(0) sent(1) me(2) the(3) memo(1) app(2) 
self(1) time(1) 
call(1) the(3) rom(1) 

您需要迭代
匹配
字符串中的每个单词。这可以通过拆分(在空间上)来实现

另一个妙招是打印出每个单词,并将可选的
end
参数设置为一个空格(
'
)。这意味着它们都停留在一行上,直到发出一个普通的
print()
调用并移动到下一行

for textmatch in match:
    for word in textmatch.split():
        num=text.count(word)
        print(word + '(' + str(num) + ')', end=' ')
    print()
在解释器中测试:

>>> text= ''' One of the best ways to make yourself happy in the present is to 
...            recall happy times from the past memories '''
>>> 
>>> match=['happy birthday to me' , 'john sent me the memo app' , 'self time' , 'call the rom']
>>> for textmatch in match:
...     for word in textmatch.split():
...         num=text.count(word)
...         print(word + '(' + str(num) + ')', end=' ')
...     print()
... 
happy(2) birthday(0) to(2) me(2) 
john(0) sent(1) me(2) the(3) memo(1) app(2) 
self(1) time(1) 
call(1) the(3) rom(1) 

您可以进行列表理解,形成单词列表及其频率:

text= ''' One of the best ways to make yourself happy in the present is to recall happy times from the past memories '''
match=['happy birthday to me' , 'john sent me the memo app' , 'self time' , 'call the rom']

print([[(y, text.count(y)) for y in x.split()] for x in match])

您可以进行列表理解,形成单词列表及其频率:

text= ''' One of the best ways to make yourself happy in the present is to recall happy times from the past memories '''
match=['happy birthday to me' , 'john sent me the memo app' , 'self time' , 'call the rom']

print([[(y, text.count(y)) for y in x.split()] for x in match])

它工作得很好。。但是,你能不能让这篇文章更简洁一点呢?。比如,祝我生日快乐(0)@RaviSRanjan,如果你必须对结果进行任何操作,你需要牺牲可读性。它工作得非常完美。。但是,你能不能让这篇文章更简洁易读……比如说,祝我生日快乐(0)@RaviSRanjan,如果你不得不对结果进行任何操作,你需要牺牲可读性。