简体   繁体   English

使用字典理解过滤python字典

[英]Filter python dictionary with dictionary-comprehension

I have a dictionary that is really a geojson: 我有一本真的是geojson的字典:

points = {
    'crs': {'properties': {'name': 'urn:ogc:def:crs:OGC:1.3:CRS84'}, 'type': 'name'},
    'features': [
        {'geometry': {
            'coordinates':[[[-3.693162104185235, 40.40734504903418],
                            [-3.69320229317164, 40.40719570724241],
                            [-3.693227952841606, 40.40698546120488],
                            [-3.693677594635894, 40.40712700492216]]],
            'type': 'Polygon'},
         'properties': {
             'name': 'place1',
             'temp': 28},
         'type': 'Feature'
        },
        {'geometry': {
            'coordinates': [[[-3.703886381691941, 40.405197271972035],
                             [-3.702972834622821, 40.40506272989243],
                             [-3.702552994966045, 40.40506798079752],
                             [-3.700985024825222, 40.405500820623814]]],
            'type': 'Polygon'},
         'properties': {
             'name': 'place2',
             'temp': 27},
         'type': 'Feature'
        },
        {'geometry': {
            'coordinates': [[[-3.703886381691941, 40.405197271972035],
                             [-3.702972834622821, 40.40506272989243],
                             [-3.702552994966045, 40.40506798079752],
                             [-3.700985024825222, 40.405500820623814]]],
            'type': 'Polygon'},
         'properties': {
             'name': 'place',
             'temp': 25},
         'type': 'Feature'
        }
    ],
    'type': u'FeatureCollection'
}

I would like to filter it to stay only with places that have a specific temperature, for example, more than 25 degrees Celsius. 我想对其进行过滤,以使其仅停留在具有特定温度(例如,高于25摄氏度)的地方。

I have managed to do it this way: 我设法做到这一点:

dict(crs = points["crs"],
     features = [i for i in points["features"] if i["properties"]["temp"] > 25],
     type = points["type"])

But I wondered if there was any way to do it more directly, with dictionary comprehension. 但是我想知道是否有任何方法可以通过字典理解来更直接地做到这一点。

Thank you very much. 非常感谢你。

I'm very late. 我来晚了 A dict compreheneison won't help you since you have only three keys. 字典理解对您没有帮助,因为您只有三个键。 But if you meet the following conditions: 1. you don't need a copy of features (eg your dict is read only); 但是,如果满足以下条件,则:1.您不需要features副本(例如,您的字典是只读的); 2. you don't need index access to features , you my use a generator comprehension instead of a list comprehension: 2.不需要对features索引访问,可以使用生成器理解而不是列表理解:

dict(crs = points["crs"],
                features = (i for i in points["features"] if i["properties"]["temp"] > 25),
                type = points["type"])

The generator is created in constant time, while the list comprehension is created in O(n). 生成器是在固定时间内创建的,而列表推导是在O(n)中创建的。 Furthermore, if you create a lot of those dicts, you have only one copy of the features in memory. 此外,如果您创建大量这些命令,则内存中将只有一份features副本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM