Найти - Пользователи
Полная версия: Регулярные выражения
Начало » Python для новичков » Регулярные выражения
1 2
@mary@
Если в тексте есть слова, которые повторяются, как их найти и вывести в отдельный список?
terabayt
>>> import re
>>> s = """ou may wish to load the Maps API JavaScript code after your page has finished loading, or on demand. To do so, you can inject your own <script> tag in response to a window.onload event or a function call, but you need to additionally instruct the Maps JavaScript API bootstrap to delay execution of your application code until the Maps JavaScript API code is fully loaded. You may do so using the callback parameter, which takes as an argument the function to execute upon completing loading the API"""
>>> l = re.split('\W*', s)
>>> a = []
>>> for i, j in enumerate(l):
...     if j not in a and j in l[:i]:
...         a.append(j)
... 
>>> print(a)
['your', 'to', 'or', 'a', 'you', 'the', 'Maps', 'JavaScript', 'API', 'code', 'may', 'do', 'so', 'function', 'loading']
@mary@
спасибо)
хотела бы еще спросить, в res хочу чтобы вывело 3 первых слова
[code import re
res =
for line in “манна, осанна, мумма, кллорд” :
linew = re.findall(r“мм|нн”, line)
res.extend(linew)]
а выводит пустой список

а такой вариант
text = “манна, осанна, мумма, кллорд”
rese = lambda text: re.findall(r'мм|нн', text)
выводит сам шаблон
rese(text)

terabayt
@mary@
в res хочу чтобы вывело 3 первых слова
вставьте правильно код
@mary@
 import re
res = []
for line in “манна, осанна, мумма, кллорд” :
     linew = re.findall(r“мм|нн”, line)
     res.extend(linew)]
@mary@
text = “манна, осанна, мумма, кллорд”
rese = lambda text: re.findall(r'мм|нн', text)
>>>rese(text)
['нн', 'нн', 'мм']
@mary@
а мне нужно чтобы слово полностью выводилось
terabayt
>>> import re
>>> text = "манна, осанна, мумма, кллорд".decode('utf-8')
>>> l = re.findall(u'(?u)(\w*[мм|нн]\w*)', text)
>>> for i in l:
...     print i.encode('utf-8')
... 
манна
осанна
мумма
@mary@
хотела бы спросить, зачем мы меняем кодировку?
@mary@
у меня пошло без кодировки и с + вместо *
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