Вы можете определить пользовательскую функцию, применить ее к каждой строке через map
, а затем передать dict
:
L = ['@David, the age is 27', '@John, the age is 99', '@rain, the age is 45']
def key_value_extractor(x):
x_split = x.split(',') # split by ','
name = x_split[0][1:] # take 1st split and exclude first character
age = int(x_split[1].rsplit(maxsplit=1)[-1]) # take 2nd, right-split, convert to int
return name, age
res = dict(map(key_value_extractor, L))
{'David': 27, 'John': 99, 'rain': 45}