Использование регулярного выражения только для получения списковPython

Программы на Python
Anonymous
Использование регулярного выражения только для получения списков

Сообщение Anonymous »

Я пытаюсь получить списки в тексте ниже.
import re

# Read the content from the file (here we assume the content is stored in a string for demonstration)
content = """
Variation 1:
Based on the provided examples and the input sequence, the next anticipated actions for the input sequence [['Start'], ['Pick', 'mixing_bowl_green']] could be:

1. Pick the sponge (since wiping actions typically require the sponge).
2. Wipe the mixing bowl.
3. Place the mixing bowl.
4. Place the sponge.
5. Pick the broom.
6. Sweep.
7. Place the broom.
8. End the sequence.

Here is the completed sequence:

[['Start'], ['Pick', 'mixing_bowl_green'], ['Pick', 'sponge_small'], ['Wipe mixing_bowl_green'], ['Place', 'mixing_bowl_green'], ['Place', 'sponge_small'], ['Pick', 'broom'], ['Sweep'], ['Place', 'broom'], ['End']]

Variation 2:
Based on the provided examples, the anticipated sequence of actions for the given input can be completed as follows:

Input = [['Start'], ['Pick', 'mixing_bowl_green']]

1. The next logical step is to pick the sponge, as it is required for the wiping actions.
2. Then, proceed to wipe the mixing bowl.
3. Place the mixing bowl back.
4. Pick the cutting board and wipe it.
5. Place the cutting board back.
6. Pick the plate and wipe it.
7. Place the plate back.
8. Place the sponge back.
9. Pick the broom and sweep.
10. Place the broom back.
11. End the sequence.

Here is the completed sequence:

[['Start'],
['Pick', 'mixing_bowl_green'],
['Pick', 'sponge_small'],
['Wipe mixing_bowl_green'],
['Place', 'mixing_bowl_green'],
['Pick', 'cutting_board_small'],
['Wipe cutting_board_small'],
['Place', 'cutting_board_small'],
['Pick', 'plate_dish'],
['Wipe plate_dish'],
['Place', 'plate_dish'],
['Place', 'sponge_small'],
['Pick', 'broom'],
['Sweep'],
['Place', 'broom'],
['End']]
"""

# Define the regular expression pattern
pattern = re.compile(r"\[\['Start'\].*?\['End'\]\]", re.DOTALL)

# Find all matches in the content
matches = pattern.findall(content)

# Print the matches
for match in matches:
print("#####")
print(match)
print("#####")
input()

Однако код возвращает мне
#####
[['Start'], ['Pick', 'mixing_bowl_green']] could be:

1. Pick the sponge (since wiping actions typically require the sponge).
2. Wipe the mixing bowl.
3. Place the mixing bowl.
4. Place the sponge.
5. Pick the broom.
6. Sweep.
7. Place the broom.
8. End the sequence.

Here is the completed sequence:

[['Start'], ['Pick', 'mixing_bowl_green'], ['Pick', 'sponge_small'], ['Wipe mixing_bowl_green'], ['Place', 'mixing_bowl_green'], ['Place', 'sponge_small'], ['Pick', 'broom'], ['Sweep'], ['Place', 'broom'], ['End']]
#####

как первое неверное совпадение. Как написать регулярное выражение, чтобы оно соответствовало только спискам? Текст между ['Начало'] и ['Конец'] должен напоминать список, то есть запятую, за которой следуют квадратные скобки. Результат должен быть
list1 = [['Start'], ['Pick', 'mixing_bowl_green'], ['Pick', 'sponge_small'], ['Wipe mixing_bowl_green'], ['Place', 'mixing_bowl_green'], ['Place', 'sponge_small'], ['Pick', 'broom'], ['Sweep'], ['Place', 'broom'], ['End']]
list2 = [['Start'],
['Pick', 'mixing_bowl_green'],
['Pick', 'sponge_small'],
['Wipe mixing_bowl_green'],
['Place', 'mixing_bowl_green'],
['Pick', 'cutting_board_small'],
['Wipe cutting_board_small'],
['Place', 'cutting_board_small'],
['Pick', 'plate_dish'],
['Wipe plate_dish'],
['Place', 'plate_dish'],
['Place', 'sponge_small'],
['Pick', 'broom'],
['Sweep'],
['Place', 'broom'],
['End']]


Подробнее здесь: https://stackoverflow.com/questions/785 ... ieve-lists

Вернуться в «Python»