Python 在字典中适当地拆分列表和子列表
我有一本这样的字典Python 在字典中适当地拆分列表和子列表,python,loops,dictionary,Python,Loops,Dictionary,我有一本这样的字典 {country: [coordinate_set1, coordinate_set2, coordinate_set3]} 其中,每个坐标_集当前是一个长串的坐标对组合在一起 如何访问每个坐标集,并将它们拆分为每对x\u坐标的列表;y_coord {country: [[set1_pair1][set1_pair2][set1_pair...], [set2_pair1][set2_pair2][set2_
{country: [coordinate_set1,
coordinate_set2,
coordinate_set3]}
其中,每个坐标_集当前是一个长串的坐标对组合在一起
如何访问每个坐标集
,并将它们拆分为每对x\u坐标的列表;y_coord
{country: [[set1_pair1][set1_pair2][set1_pair...],
[set2_pair1][set2_pair2][set2_pair...],
[set3_pair1][set3_pair2][set3_pair...]}
{country: [[x1_1,y1_1][x1_2,y1_2][x1_...,y1_],
[x2_1,y2_1][x2_2,y2_2][x2_...,y2_],
[x3_1,y3_1][x3_2,y3_2][x3_...,y3_]}
最后,在这些列表中,为每一对x\u-coord,y\u-coord
{country: [[set1_pair1][set1_pair2][set1_pair...],
[set2_pair1][set2_pair2][set2_pair...],
[set3_pair1][set3_pair2][set3_pair...]}
{country: [[x1_1,y1_1][x1_2,y1_2][x1_...,y1_],
[x2_1,y2_1][x2_2,y2_2][x2_...,y2_],
[x3_1,y3_1][x3_2,y3_2][x3_...,y3_]}
由于这些值是以空格分隔和分号分隔的,所以在每个字符串元素上使用.split(“”
和.split(“;”)
应该相当简单,但我不知道如何索引和保存拆分
我觉得应该是类似于
for country, coordinate in dict.items():
for coordinate_set in coordinate:
split_set = coordinate_set.split(sep = " ")
for xy_set in split_set:
xy_tuple = tuple(xy_set.split(sep = ";")
但我真的不知道该如何拯救这些
样本数据:
编辑:使用示例数据更新
Antarctica
-80.0401787251;-59.5720946926 -80.5496566711;-59.865849372
-79.4970594217;-159.20818356 -79.634208673;-161.127601285
-78.0470696006;-45.1547576564 -78.4781027223;-43.9208278062
这应该做到,列表和听写理解相结合:
{country: [tuple(xy_set.split(';')) for cordinate_set in coordinate for xy_set in cordinate_set.split()] for country, coordinate in dict.items()}
101
方式:
result = {}
for country, coordinate in dict.items():
for cordinate_set in coordinate:
for xy_set in cordinate_set.split():
e = tuple(xy_set.split(';'))
if country in result:
result[country].append(e)
else:
result[country] = [e]
Python 101方式
dict1={'Antarctica':
['''-80.0401787251;-59.5720946926 -80.5496566711;-59.865849372
-79.4970594217;-159.20818356 -79.634208673;-161.127601285
-78.0470696006;-45.1547576564 -78.4781027223;-43.9208278062''']}
for country, coordinate in dict1.items():
cs=[]
for coordinate_set in coordinate:
split_set = coordinate_set.split(sep = " ")
ss=[]
for xy_set in split_set:
xy_tuple = tuple(xy_set.split(sep = ";"))
ss+=[xy_tuple]
cs+=ss
dict1[country]=cs
print(dict1)
不使用理解和更少代码行的小修改
data={'Antarctica':
["""-80.0401787251;-59.5720946926 -80.5496566711;-59.865849372
-79.4970594217;-159.20818356 -79.634208673;-161.127601285
-78.0470696006;-45.1547576564 -78.4781027223;-43.9208278062"""],'Ameriica':
["""-80.0401787251;-59.5720946926 -80.5496566711;-59.865849372
-79.4970594217;-159.20818356 -79.634208673;-161.127601285
-78.0470696006;-45.1547576564 -78.4781027223;-43.9208278062"""]}
for key,value in data.items():
#print value
updatevalue=[]
inlist=value[0].split()
for i in inlist:
updatevalue.append(tuple(i.split(';')))
data.update({key:updatevalue})
print data
如果您有样本数据,请提供。现在看。你介意用Python 101的方式来做吗,即不理解?我添加了
101
way:)这个答案中“in in”和“for for”的意思是什么?@BUFU拼写错误,或者很久以前发生的事情:)谢谢!