将统一字符串转换为字典
Convert a unified string to dictionary
我正在寻找转换字符串数据的方法,如下所示:
"Kyiv, region A, 123\nKyiv, region B, 456\nKyiv, region C, 789\nPoltava, region D, 321\nPoltava, region E, 876\nPoltava, region F, 654"
到 'dict' 有两个键
{'city':'Kiev', 'population': 123}
我可以将原始字符串的子字符串转换为以下格式的字符串列表:
[['Kyiv', 'region A', 123'], ['Kyiv', 'region B', '456'],] ...
但无法继续进行。
有人可以帮我解析这个字符串吗?
这是我目前的代码:
dic_city = {}
cities = []
population = []
cities = [a.split(',') for a in s.split('\n')]
print(cities)
用换行符分割数据得到每个条目,然后用逗号+space分割得到每个条目的城市名称和人口:
entries = data.split("\n")
result = []
for entry in entries:
city, _, pop = entry.split(", ")
result.append(dict(city=city, population=population))
print(result)
这输出:
[
{'city': 'Kyiv', 'population': '123'},
{'city': 'Kyiv', 'population': '456'},
{'city': 'Kyiv', 'population': '789'},
{'city': 'Poltava', 'population': '321'},
{'city': 'Poltava', 'population': '876'},
{'city': 'Poltava', 'population': '654'}
]
txt = "Kyiv, region A, 123\nKyiv, region B, 456\nKyiv, region C, 789\nPoltava, region D, 321\nPoltava, region E, 876\nPoltava, region F, 654"
d = {}
for x in txt.splitlines():
city = x.split(',')[0]
if d.get(city):
d[city] += int(x.split(',')[2]) # same city then add population
else:
d[city] = int(x.split(',')[2]) # different city add new key to dictionary
print(d)
输出-
{'Kyiv': 1368, 'Poltava': 1851}
试试这个
# split into nested lists
aux = [x.split(', ') for x in s.split('\n')]
# build a list of dictionaries
res = [{'city':c, 'population': int(p)} for c, _, p in aux]
res
与理解相同的代码
s = "Kyiv, region A, 123\nKyiv, region B, 456\nKyiv, region C, 789\nPoltava, region D, 321\nPoltava, region E, 876\nPoltava, region F, 654"
# create a dictionary in loop
[{'city':c, 'population': int(p)} for c, _, p in (x.split(', ') for x in s.split('\n'))]
#[{'city': 'Kyiv', 'population': 123},
# {'city': 'Kyiv', 'population': 456},
# {'city': 'Kyiv', 'population': 789},
# {'city': 'Poltava', 'population': 321},
# {'city': 'Poltava', 'population': 876},
# {'city': 'Poltava', 'population': 654}]
我正在寻找转换字符串数据的方法,如下所示:
"Kyiv, region A, 123\nKyiv, region B, 456\nKyiv, region C, 789\nPoltava, region D, 321\nPoltava, region E, 876\nPoltava, region F, 654"
到 'dict' 有两个键
{'city':'Kiev', 'population': 123}
我可以将原始字符串的子字符串转换为以下格式的字符串列表:
[['Kyiv', 'region A', 123'], ['Kyiv', 'region B', '456'],] ...
但无法继续进行。
有人可以帮我解析这个字符串吗?
这是我目前的代码:
dic_city = {}
cities = []
population = []
cities = [a.split(',') for a in s.split('\n')]
print(cities)
用换行符分割数据得到每个条目,然后用逗号+space分割得到每个条目的城市名称和人口:
entries = data.split("\n")
result = []
for entry in entries:
city, _, pop = entry.split(", ")
result.append(dict(city=city, population=population))
print(result)
这输出:
[
{'city': 'Kyiv', 'population': '123'},
{'city': 'Kyiv', 'population': '456'},
{'city': 'Kyiv', 'population': '789'},
{'city': 'Poltava', 'population': '321'},
{'city': 'Poltava', 'population': '876'},
{'city': 'Poltava', 'population': '654'}
]
txt = "Kyiv, region A, 123\nKyiv, region B, 456\nKyiv, region C, 789\nPoltava, region D, 321\nPoltava, region E, 876\nPoltava, region F, 654"
d = {}
for x in txt.splitlines():
city = x.split(',')[0]
if d.get(city):
d[city] += int(x.split(',')[2]) # same city then add population
else:
d[city] = int(x.split(',')[2]) # different city add new key to dictionary
print(d)
输出-
{'Kyiv': 1368, 'Poltava': 1851}
试试这个
# split into nested lists
aux = [x.split(', ') for x in s.split('\n')]
# build a list of dictionaries
res = [{'city':c, 'population': int(p)} for c, _, p in aux]
res
与理解相同的代码
s = "Kyiv, region A, 123\nKyiv, region B, 456\nKyiv, region C, 789\nPoltava, region D, 321\nPoltava, region E, 876\nPoltava, region F, 654"
# create a dictionary in loop
[{'city':c, 'population': int(p)} for c, _, p in (x.split(', ') for x in s.split('\n'))]
#[{'city': 'Kyiv', 'population': 123},
# {'city': 'Kyiv', 'population': 456},
# {'city': 'Kyiv', 'population': 789},
# {'city': 'Poltava', 'population': 321},
# {'city': 'Poltava', 'population': 876},
# {'city': 'Poltava', 'population': 654}]