Skip to content

Instantly share code, notes, and snippets.

@thisiswei
Created May 31, 2021 12:52
Show Gist options
  • Save thisiswei/b227809921b7b13d922fff6fe65daa96 to your computer and use it in GitHub Desktop.
Save thisiswei/b227809921b7b13d922fff6fe65daa96 to your computer and use it in GitHub Desktop.
import re
normal_regex = '[$|£]\s*\S+\s*(?:million)?'
def tmp(b):
# ['$910,0001955 ', '$350,000 ']
normal = re.findall(normal_regex, b)
return normal
def cal_average_budget(budget_list):
li = []
for b in budget_list:
price = 0
normal = re.findall(normal_regex, b)
for i in normal:
cur = i[:1]
i = i[1:].replace(',', '').replace(',', '')
for k in unit_dict.keys():
if k in i:
i = i.replace(k, '')
for c in split_c_list:
if c in i:
l = [float(o) for o in i.split(c)]
i = sum(l) / len(l)
break
price = float(i) * unit_dict[k] * multiple_dict[cur]
if not price:
price = float(i)
li.append(price)
return round(sum(li) / len(li), 2)
# 1954 (27th) - On the Waterfront - $910,000
# 1955 (28th) - Marty - $350,000 [ 1 ] [ 2 ]
lis = [
# '$910,0001955 '
'1954 (27th) - On the Waterfront - $910,000',
'1955 (28th) - Marty - $350,000 [ 1 ] [ 2 ]',
'1972 (45th) - The Godfather - $6–7 million [ 1 ] [ 2 ]',
]
for l in lis:
print(cal_average_budget(l))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment