Найти - Пользователи
Полная версия: Помогите плз!
Начало » Центр помощи » Помогите плз!
1
siperok
У меня есть переменная
sentence = ['she sells sea shells by the sea shore']
Как сделать так чтобы перед каждым словом которое начинается на sh добавлялось слово like.
Заранее спасибо за помощь.
terabayt
>>> sentence = 'she sells sea shells by the sea shore'
>>> out = ' '.join(map(lambda x: 'like ' + x if len(x) > 1 and x[:2] == 'sh' else x, sentence.split(" ")))
>>> print(out)
like she sells sea like shells by the sea like shore
siperok
Спасибо
GreyZmeem
terabayt
len(x) > 1 and x[:2] == 'sh'
x.startswith('sh')
terabayt
GreyZmeem
о, да, спасибо, постоянно забываю.
dimy44
import re
sentence = 'she sells sea shells by the sea shore'
out = re.sub(r'\bsh', 'like sh', sentence)
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB