题目描述
解析${xxx}变量
输入:
4
xxx=lyf/${ttt}/test
ttt=www
yyy=seeyou
aa=aaa/${xxx}/bbb/${yyy}/bb
输出:最后一个变量的完全解析
aaa/lyf/www/test/bbb/seeyou/bb
解法:
def modify(variable, **kwargs):
if kwargs[variable].find('${') != -1:
l = kwargs[variable].replace('${', '*{').replace('}', '}*').split('*')
var, modified_l = "", []
for i in l:
if i[0] == '{':
var = i.replace('{', '').replace('}', '')
if kwargs[var].find('${') != -1:
kwargs = modify(var, **kwargs)
modified_l.append(kwargs[var])
else:
modified_l.append(kwargs[var])
else:
modified_l.append(i)
modified_variable = "".join(modified_l)
kwargs[variable] = modified_variable
return kwargs
n = int(input())
str_dict = {}
for i in range(n):
left, right = input().split('=')
str_dict[left] = right
if i == n-1:
last_l = left
str_dict = modify(last_l, **str_dict)
print(str_dict[last_l])