1import re
2
3
4def parse_wishlist(data):
5 christmas_magic = re.compile(r'\(\s*(-?[\.\d]*)\s*\)\s*(.*?(?=\s+\[)).*\[\s*(\d+)|[-\*].*?(?=[^\s])(.*?(?=\s*(?:\n|$)))')
6 parsed_data = christmas_magic.findall(data)
7
8 wishlist = []
9 for row in parsed_data:
10 if row[3] == '':
11 wishlist.append([float(row[0]), row[1], int(row[2]), []])
12 else:
13 wishlist[-1][-1].append(row[3])
14
15 return list((item[0], item[1], item[2], tuple(item[3])) for item in wishlist)
...
----------------------------------------------------------------------
Ran 3 tests in 0.003s
OK
f | 1 | import re | f | 1 | import re |
2 | 2 | ||||
3 | 3 | ||||
4 | def parse_wishlist(data): | 4 | def parse_wishlist(data): | ||
5 | christmas_magic = re.compile(r'\(\s*(-?[\.\d]*)\s*\)\s*(.*?(?=\s+\[)).*\[\s*(\d+)|[-\*].*?(?=[^\s])(.*?(?=\s*(?:\n|$)))') | 5 | christmas_magic = re.compile(r'\(\s*(-?[\.\d]*)\s*\)\s*(.*?(?=\s+\[)).*\[\s*(\d+)|[-\*].*?(?=[^\s])(.*?(?=\s*(?:\n|$)))') | ||
n | 6 | data = christmas_magic.findall(data) | n | 6 | parsed_data = christmas_magic.findall(data) |
7 | 7 | ||||
8 | wishlist = [] | 8 | wishlist = [] | ||
t | 9 | for row in data: | t | 9 | for row in parsed_data: |
10 | if row[3] == '': | 10 | if row[3] == '': | ||
11 | wishlist.append([float(row[0]), row[1], int(row[2]), []]) | 11 | wishlist.append([float(row[0]), row[1], int(row[2]), []]) | ||
12 | else: | 12 | else: | ||
13 | wishlist[-1][-1].append(row[3]) | 13 | wishlist[-1][-1].append(row[3]) | ||
14 | 14 | ||||
15 | return list((item[0], item[1], item[2], tuple(item[3])) for item in wishlist) | 15 | return list((item[0], item[1], item[2], tuple(item[3])) for item in wishlist) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
27.12.2023 12:22