使用预定义的键创建嵌套字典,但在 python 中没有值
Create nested dictionary with predefined keys but without values in python
瞄准
对于作业,我需要输入一些建筑信息(几何图形及其属性 ) 转换为 GeoJSON 数据结构。
方法
我一般的做法是:
使用必要的 GeoJSON 数据结构和 .append
该结构的数据创建一个空字典
(见:下文)
output_buildings = {
'type': "FeatureCollection",
'features': [
{
'type': "Feature",
'geometry': {
'type': ,
'coordinates':
},
'properties': {
'building_id': ,
'building_year': ,
'building_status': ,
'building_occurance':
}
}
]
}
问题
我知道如何创建一个简单的空字典,但我的问题是我不知道如何创建一个键结构没有值(因为我想稍后附加这些值)。我在 ,
的 type
(features
中的第二个)收到一个错误。
之前的努力
- 我在 Whosebug 上找到了这个主题:
Method for Creating a Nested Dictionary from a List of Keys;
但它不符合我的目的。
- 我在 python 文档中搜索了 "create empty nested dictionary"、"create dictionary with empty values" 等术语,但没有找到我要找的内容。
- 我尝试使用占位符 (%s / %d),但它们不适合这个目的
- 最后我尝试使用
pass
(没用)。
问题
我还没有找到解决办法。你能给我一些建议吗?
提前致谢!
您当前的字典结构无效,因为您必须存在 key-value 对。
您可以通过为没有指定值的键填写 None
占位符来解决这个问题。但是,键 coordinates
可以设置为空列表,因为您可以有多个坐标。
有效的 GeoJSON 结构:
output_buildings = {
'type': "FeatureCollection",
'features': [
{
'type': "Feature",
'geometry': {
'type': None,
'coordinates': []
},
'properties': {
'building_id': None,
'building_year': None,
'building_status': None,
'building_occurance': None
}
}
]
}
瞄准
对于作业,我需要输入一些建筑信息(几何图形及其属性 ) 转换为 GeoJSON 数据结构。
方法
我一般的做法是:
使用必要的 GeoJSON 数据结构和 .append
该结构的数据创建一个空字典
(见:下文)
output_buildings = {
'type': "FeatureCollection",
'features': [
{
'type': "Feature",
'geometry': {
'type': ,
'coordinates':
},
'properties': {
'building_id': ,
'building_year': ,
'building_status': ,
'building_occurance':
}
}
]
}
问题
我知道如何创建一个简单的空字典,但我的问题是我不知道如何创建一个键结构没有值(因为我想稍后附加这些值)。我在 ,
的 type
(features
中的第二个)收到一个错误。
之前的努力
- 我在 Whosebug 上找到了这个主题: Method for Creating a Nested Dictionary from a List of Keys; 但它不符合我的目的。
- 我在 python 文档中搜索了 "create empty nested dictionary"、"create dictionary with empty values" 等术语,但没有找到我要找的内容。
- 我尝试使用占位符 (%s / %d),但它们不适合这个目的
- 最后我尝试使用
pass
(没用)。
问题
我还没有找到解决办法。你能给我一些建议吗?
提前致谢!
您当前的字典结构无效,因为您必须存在 key-value 对。
您可以通过为没有指定值的键填写 None
占位符来解决这个问题。但是,键 coordinates
可以设置为空列表,因为您可以有多个坐标。
有效的 GeoJSON 结构:
output_buildings = {
'type': "FeatureCollection",
'features': [
{
'type': "Feature",
'geometry': {
'type': None,
'coordinates': []
},
'properties': {
'building_id': None,
'building_year': None,
'building_status': None,
'building_occurance': None
}
}
]
}