将统一字符串转换为字典



我正在寻找转换字符串数据的方法,看起来像以下:

"Kyiv, region A, 123nKyiv, region B, 456nKyiv, region C, 789nPoltava, region D, 321nPoltava, region E, 876nPoltava, 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)
txt = "Kyiv, region A, 123nKyiv, region B, 456nKyiv, region C, 789nPoltava, region D, 321nPoltava, region E, 876nPoltava, 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}

用换行符分隔数据以获得每个条目,然后用逗号+空格分隔数据以获得每个条目的城市名称和人口:

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'}
]

试试这个

# 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, 123nKyiv, region B, 456nKyiv, region C, 789nPoltava, region D, 321nPoltava, region E, 876nPoltava, 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}]

相关内容

  • 没有找到相关文章

最新更新