Разделить список на подсписки элементов между открывающими и закрывающими токенами

Для PHP, «всегда безопасно предшествовать не-буквенно-цифровому с« \ », чтобы указать, что он обозначает себя». - http://php.net/manual/en/regexp.reference.escape.php .

За исключением случаев, когда это «или».: /

Чтобы избежать переменных шаблона регулярных выражений (или частичных переменных) в PHP, используйте preg_quote ()

2
задан moooeeeep 18 January 2019 в 10:08
поделиться

7 ответов

с помощью регулярных выражений вы можете сделать это следующим образом.

a= ['RATED','  Awesome food at a good price .', 
 'Delivery was very quick even on New Year’s Eve .', 
 'Please try crispy corn and veg noodles From this place .', 
 'Taste maintained .', 'Like', '1', 'Comment', '0', 
 'Share', 'Divyansh Agarwal', '1 Review', 'Follow', 
 '3 days ago', 'RATED', 
 '  I have tried schezwan noodles and the momos with kitkat shake', "And I would say just one word it's best for the best reasonable rates.... Gotta recommend it to everyone", 
 'Like']


import re
string = ' '.join(a)
b = re.compile(r'(?<=RATED).*?(?=Like)').findall(string)
print(b)

output

['   Awesome food at a good price . Delivery was very quick even on New Year’s Eve . Please try crispy corn and veg noodles From this place . Taste maintained . ',
 "   I have tried schezwan noodles and the momos with kitkat shake And I would say just one word it's best for the best reasonable rates.... Gotta recommend it to everyone "]
0
ответ дан prashant rana 18 January 2019 в 10:08
поделиться

Опция без флагов:

new_list = []
group = [] # don’t need if the list starts with 'RATED'

for i in your_list:
    if i == 'RATED':
        group = []
    elif i == 'Like':
        new_list.append(group[:])
    else:
        group.append(i)
0
ответ дан Mykola Zotko 18 January 2019 в 10:08
поделиться

Вы можете попробовать, например,

rec = False
result = []
for s in lst:
    if s == 'Like':
        rec = False
    if rec:
        result.append(s)
    if s == 'RATED':
        rec = True

результат

#[' Awesome food at a good price .',
# 'Delivery was very quick even on New Year’s Eve .',
# 'Please try crispy corn and veg noodles From this place .',
# 'Taste maintained .',
# ' I have tried schezwan noodles and the momos with kitkat shake',
# "And I would say just one word it's best for the best reasonable rates.... Gotta recommend it to everyone"]
0
ответ дан SpghttCd 18 January 2019 в 10:08
поделиться

Я бы посоветовал вам узнать о поиске и разрезании индекса по типам последовательностей:

Пример:

def group_between(lst, start_token, end_token):
    while lst:
        try:
            # find opening token
            start_idx = lst.index(start_token) + 1
            # find closing token
            end_idx = lst.index(end_token, start_idx)
            # output sublist
            yield lst[start_idx:end_idx]
            # continue with the remaining items
            lst = lst[end_idx+1:]
        except ValueError:
            # begin or end not found, just skip the rest
            break

l = ['RATED','  Awesome food at a good price .', 'Delivery was very quick even on New Year’s Eve .', 'Please try crispy corn and veg noodles From this place .', 'Taste maintained .', 'Like', 
     '1', 'Comment', '0', 'Share', 'Divyansh Agarwal', '1 Review', 'Follow', '3 days ago',
     'RATED', '  I have tried schezwan noodles and the momos with kitkat shake', "And I would say just one word it's best for the best reasonable rates.... Gotta recommend it to everyone", 'Like'
]

for i in group_between(l, 'RATED', 'Like'):
    print(i)

Вывод:

['  Awesome food at a good price .', 'Delivery was very quick even on New Year’s Eve .', 'Please try crispy corn and veg noodles From this place .', 'Taste maintained .']
['  I have tried schezwan noodles and the momos with kitkat shake', "And I would say just one word it's best for the best reasonable rates.... Gotta recommend it to everyone"]
0
ответ дан moooeeeep 18 January 2019 в 10:08
поделиться

Вы могли бы использовать регулярное выражение. Сначала вам нужно объединить свой список с некоторым разделителем, который не будет встречаться в тексте. вам просто нужно перебрать все результаты и разделить строку с разделителем

for s in results:
    print(s.split(delimiter))
0
ответ дан Luka Dumančić 18 January 2019 в 10:08
поделиться

Вы можете использовать приведенный ниже код, который использует простой цикл for:

l = ['RATED','  Awesome food at a good price .', 'Delivery was very quick even on New Year’s Eve .', 'Please try crispy corn and veg noodles From this place .', 'Taste maintained .', 'Like', 
     '1', 'Comment', '0', 'Share', 'Divyansh Agarwal', '1 Review', 'Follow', '3 days ago',
     'RATED', '  I have tried schezwan noodles and the momos with kitkat shake', "And I would say just one word it's best for the best reasonable rates.... Gotta recommend it to everyone", 'Like'
]

st, ed, aa = None, None, []
for k, v in enumerate(l):
    if v == "RATED":
        st = k
    if v == "Like":
        ed = k
    if st != None and ed!= None:
        aa.extend(l[st+1: ed])
        st = None
        ed = None

print (aa)

# ['  Awesome food at a good price .', 'Delivery was very quick even on New Year’s Eve .', 'Please try crispy corn and veg noodles From this place .', 'Taste maintained .', '  I have tried schezwan noodles and the momos with kitkat shake', "And I would say just one word it's best for the best reasonable rates.... Gotta recommend it to everyone"]
0
ответ дан Akash Swain 18 January 2019 в 10:08
поделиться
def find_between(old_list, first_word, last_word):
    new_list = []
    flag = False
    for i in old_list:
        if i is last_word:
            break
        if i is first_word:
            flag = True
            continue
        if flag:
            new_list.append(i)
    return new_list
0
ответ дан Kranthi Kiran 18 January 2019 в 10:08
поделиться
Другие вопросы по тегам:

Похожие вопросы: