找到所有匹配的单词并获得它们的数量
Find all words that match and get the number of them
我的代码应该使用 while 循环打印从 Z 到 Y 替换的所有单词的数量。
paragraph_list = ["She looked at her ztudent wondering if zhe could ever get through. You need to learn to think for yourself, she wanted to tell him. Your friends are holding you zack and bringing you down.",
"I recently discovered I could make fudge with just chocolate chips, zweetened condenzed milk, vanilla extract, and a thick pot on slow heat. I tried it with dark chocolate chunkz and I tried it with semi-sweet chocolate chips. It's better with both kinds. It comes out pretty bad with just the dark chocolate. The best add-ins are crushed almonds and marshmallows -- what you get from that is Rocky Road.","The speciez are the Plains Zebra, which is the most common one, the Mountain Zebra, and the Grevy Zebra. Zebras are a short, stocky animal that is generally about 8 feet long and stands between 4 and 5 feet at the shoulder. They can weigh up to 650 pounds. The stripes on a zebra are very much like fingerprints."]
def findZ():
for string in paragraph_list:
print(string.replace('Z', 'Y').replace('z', 'y'))
findZ()
使用 sum
和 count
以及列表理解
sum(sent.lower().count('z') for sent in paragraph_list)
我的代码应该使用 while 循环打印从 Z 到 Y 替换的所有单词的数量。
paragraph_list = ["She looked at her ztudent wondering if zhe could ever get through. You need to learn to think for yourself, she wanted to tell him. Your friends are holding you zack and bringing you down.",
"I recently discovered I could make fudge with just chocolate chips, zweetened condenzed milk, vanilla extract, and a thick pot on slow heat. I tried it with dark chocolate chunkz and I tried it with semi-sweet chocolate chips. It's better with both kinds. It comes out pretty bad with just the dark chocolate. The best add-ins are crushed almonds and marshmallows -- what you get from that is Rocky Road.","The speciez are the Plains Zebra, which is the most common one, the Mountain Zebra, and the Grevy Zebra. Zebras are a short, stocky animal that is generally about 8 feet long and stands between 4 and 5 feet at the shoulder. They can weigh up to 650 pounds. The stripes on a zebra are very much like fingerprints."]
def findZ():
for string in paragraph_list:
print(string.replace('Z', 'Y').replace('z', 'y'))
findZ()
使用 sum
和 count
以及列表理解
sum(sent.lower().count('z') for sent in paragraph_list)