#python #while-loop
Вопрос:
Мой код должен вывести количество всех слов, замененных с Z на Y, используя цикл while.
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()
Комментарии:
1. Есть ли несколько элементов внутри
paragraph_list
или это просто строка?2. Считать слова с
Z
помощью s?3. @It_is_Chris да
4. @Vishnudev Я выясняю, может ли он считать / возвращать все слова с буквами Y и получать их количество
5. Вы не можете считать буквы y после этого, потому что в строке могли быть буквы y до преобразования. Вы должны сначала сосчитать буквы «z».
Ответ №1:
Используйте sum
и count
с пониманием списка
sum(sent.lower().count('z') for sent in paragraph_list)