Skip to main content
 首页 » 编程设计

python之如何计算忽略大小写的字符串中每个单词中字符的出现次数

2025年05月04日28JustinYoung

<分区>

我有一个字符串,说 Welcome to this world。我需要计算字符 w 的出现次数,忽略大小写。

x = "Welcome to this world" 
list = x.split() 
print(list) 
a = [] 
 
for i in list: 
    a.append(i.count("w")) 
else: 
    print(a) 

Output: ['Welcome', 'to', 'this', 'world'] [0,0,0,1]

这将输出作为 [0,0,0,1] 但我希望它是 [1,0,0,1] 如果 w 被忽略。我应该做出哪些改变?