在python字典中将值从一个键转移到另一个键

Sam*_*mar 5 python dictionary geojson data-structures

我有以下字典(Geojson):

'properties': {
            'fill': '#ffffff', 'fill-opacity': 1, 'stroke': '#ffffff',
'stroke-opacity': 1, 'stroke-width': 1.5, 'title': '0.00 m',
'time': '2000-01-31'
    }
Run Code Online (Sandbox Code Playgroud)

通过将某些值移动到属性中的新键,将是最简单的方法如下。

'properties': {
        'style': {
            'fill': '#ffffff', 'fill-opacity': 1, 'stroke': '#ffffff',
'stroke-opacity': 1, 'stroke-width': 1.5, 'title': '0.00 m'
        },
        'time': '2000-01-31'
    }
}
Run Code Online (Sandbox Code Playgroud)

任何反馈将有所帮助。谢谢。

Thi*_*lle 3

你可以弹出并构建一个新的字典,time如下所示:

properties = {
            'fill': '#ffffff', 'fill-opacity': 1, 'stroke': '#ffffff',
'stroke-opacity': 1, 'stroke-width': 1.5, 'title': '0.00 m',
'time': '2000-01-31'
    }

time = properties.pop('time')
new_properties = {'style': properties, 'time':time}

print(new_properties)
# {'style': {'fill': '#ffffff', 'fill-opacity': 1, 'stroke': '#ffffff', 
#            'stroke-opacity': 1, 'stroke-width': 1.5, 'title': '0.00 m'},
#   'time': '2000-01-31'}
Run Code Online (Sandbox Code Playgroud)