Как составить регулярное выражение?

Здравствуйте!
Имеется строка:
[{'slot': -1, 'team': 2, 'time': 576.41, 'type': 'CHAT_MESSAGE_TOWER_KILL'},
 {'slot': -1, 'team': 2, 'time': 791.491, 'type': 'CHAT_MESSAGE_TOWER_KILL'},
 {'player_slot': 129,
  'slot': 6,
  'team': 3,
  'time': 1187.728,
  'type': 'CHAT_MESSAGE_TOWER_KILL'},
 {'player_slot': 4,
  'slot': 4,
  'team': 2,
  'time': 1237.115,
  'type': 'CHAT_MESSAGE_TOWER_KILL'},
 {'player_slot': 129,
  'slot': 6,
  'team': 3,
  'time': 1366.95,
  'type': 'CHAT_MESSAGE_TOWER_KILL'},
 {'slot': -1, 'team': 2, 'time': 1621.088, 'type': 'CHAT_MESSAGE_TOWER_KILL'},
 {'player_slot': 130,
  'slot': 7,
  'team': 3,
  'time': 1655.347,
  'type': 'CHAT_MESSAGE_TOWER_KILL'},
 {'player_slot': 132,
  'slot': 9,
  'team': 3,
  'time': 1686.872,
  'type': 'CHAT_MESSAGE_TOWER_KILL'},
 {'player_slot': 2,
  'slot': 2,
  'team': 2,
  'time': 1751.123,
  'type': 'CHAT_MESSAGE_TOWER_KILL'},
 {'player_slot': 4,
  'slot': 4,
  'team': 2,
  'time': 1780.849,
  'type': 'CHAT_MESSAGE_TOWER_KILL'},
 {'slot': -1, 'team': 3, 'time': 1927.636, 'type': 'CHAT_MESSAGE_TOWER_KILL'},
 {'player_slot': 129,
  'slot': 6,
  'team': 3,
  'time': 2117.449,
  'type': 'CHAT_MESSAGE_TOWER_KILL'},
 {'player_slot': 132,
  'slot': 9,
  'team': 3,
  'time': 2156.047,
  'type': 'CHAT_MESSAGE_TOWER_KILL'},
 {'key': '512', 'time': 2178.992, 'type': 'CHAT_MESSAGE_BARRACKS_KILL'},
 {'player_slot': 4,
  'slot': 4,
  'team': 2,
  'time': 2326.829,
  'type': 'CHAT_MESSAGE_TOWER_KILL'},
 {'key': '2', 'time': 2333.384, 'type': 'CHAT_MESSAGE_BARRACKS_KILL'}]


Необходимо:
1. Проверить есть ли в этой строке подстрока, где 'type': 'CHAT_MESSAGE_BARRACKS_KILL'.
2. Если есть, то найти первую предыдущую подстроку, где 'team' = 2 или 3.

Как это можно реализовать?
  • Вопрос задан
  • 138 просмотров
Пригласить эксперта
Ответы на вопрос 1
NeiroNx
@NeiroNx
Программист
Ох уж эти дотеры, совсем обленились...
import json
data = json.loads("""[{'slot': -1, 'team': 2, 'time': 576.41, 'type': 'CHAT_MESSAGE_TOWER_KILL'},
 {'slot': -1, 'team': 2, 'time': 791.491, 'type': 'CHAT_MESSAGE_TOWER_KILL'},
 {'player_slot': 129,
  'slot': 6,
  'team': 3,
  'time': 1187.728,
  'type': 'CHAT_MESSAGE_TOWER_KILL'},
 {'player_slot': 4,
  'slot': 4,
  'team': 2,
  'time': 1237.115,
  'type': 'CHAT_MESSAGE_TOWER_KILL'},
 {'player_slot': 129,
  'slot': 6,
  'team': 3,
  'time': 1366.95,
  'type': 'CHAT_MESSAGE_TOWER_KILL'},
 {'slot': -1, 'team': 2, 'time': 1621.088, 'type': 'CHAT_MESSAGE_TOWER_KILL'},
 {'player_slot': 130,
  'slot': 7,
  'team': 3,
  'time': 1655.347,
  'type': 'CHAT_MESSAGE_TOWER_KILL'},
 {'player_slot': 132,
  'slot': 9,
  'team': 3,
  'time': 1686.872,
  'type': 'CHAT_MESSAGE_TOWER_KILL'},
 {'player_slot': 2,
  'slot': 2,
  'team': 2,
  'time': 1751.123,
  'type': 'CHAT_MESSAGE_TOWER_KILL'},
 {'player_slot': 4,
  'slot': 4,
  'team': 2,
  'time': 1780.849,
  'type': 'CHAT_MESSAGE_TOWER_KILL'},
 {'slot': -1, 'team': 3, 'time': 1927.636, 'type': 'CHAT_MESSAGE_TOWER_KILL'},
 {'player_slot': 129,
  'slot': 6,
  'team': 3,
  'time': 2117.449,
  'type': 'CHAT_MESSAGE_TOWER_KILL'},
 {'player_slot': 132,
  'slot': 9,
  'team': 3,
  'time': 2156.047,
  'type': 'CHAT_MESSAGE_TOWER_KILL'},
 {'key': '512', 'time': 2178.992, 'type': 'CHAT_MESSAGE_BARRACKS_KILL'},
 {'player_slot': 4,
  'slot': 4,
  'team': 2,
  'time': 2326.829,
  'type': 'CHAT_MESSAGE_TOWER_KILL'},
 {'key': '2', 'time': 2333.384, 'type': 'CHAT_MESSAGE_BARRACKS_KILL'}]""".replace("'",'"'))
for i,e in enumerate(data):
	if e['type'] == 'CHAT_MESSAGE_BARRACKS_KILL':
		print(data[i-1])
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы