|
50鱼币
有这样一个嵌套字典的数组:
[{'type': 'Feature',
'geometry': {'type': 'Point',
'coordinates': [121.57870893028795, 40.832029370655775]},
'id': '0',
'properties': {'NDVI': 0.12189264346507125,
'NDVI_1': 0.10863204096561799,
'NDVI_2': 0.11775980552568834,
'NDVI_3': 0.060028843380444216,
'NDVI_4': 0.124300801576468,
'NDVI_5': 0.07360145291179786,
'NIRv': 0.016228177087722263,
'NIRv_1': 0.013993707937088493,
'NIRv_2': 0.014078184750596039,
'NIRv_3': 0.006894162590135567,
'NIRv_4': 0.017020508759865764,
'NIRv_5': 0.008266731187420858,
'SPCAI': -0.0031933297150000075,
'SPCAI_1': -0.011904034135000012,
'SPCAI_2': -0.008930453827500013,
'SPCAI_3': -0.01018435705500001,
'SPCAI_4': -0.0098443409775,
'SPCAI_5': -0.013190027717499997,
'landcover': 3}},
{'type': 'Feature',
'geometry': {'type': 'Point',
'coordinates': [121.84930925332695, 40.808922548991774]},
'id': '1',
'properties': {'NDVI': -0.01170994405944728,
'NDVI_1': -0.29900439916647387,
'NDVI_2': 0.03976861894432382,
'NDVI_3': 0.09965224710390498,
'NDVI_4': -0.1796938389138315,
'NDVI_5': -0.1565422208431474,
'NIRv': -0.001386076803456626,
'NIRv_1': -0.016973732229682795,
'NIRv_2': 0.004574981923355011,
'NIRv_3': 0.012368338649301164,
'NIRv_4': -0.013625285335641269,
'NIRv_5': -0.011701922363577375,
'SPCAI': -0.006302725564999976,
'SPCAI_1': -0.018470446295,
'SPCAI_2': -0.006794088497500023,
'SPCAI_3': -0.006333243085000013,
'SPCAI_4': -0.010001931569999994,
'SPCAI_5': -0.012728169947500015,
'landcover': 3}},
{'type': 'Feature',
'geometry': {'type': 'Point',
'coordinates': [121.8183046611695, 40.798971922909935]},
'id': '2',
'properties': {'NDVI': 0.01737915874796782,
'NDVI_1': -0.3890142964635066,
'NDVI_2': 0.06455640881563349,
'NDVI_3': 0.07612337560670104,
'NDVI_4': -0.20638756789064716,
'NDVI_5': -0.30888643576652425,
'NIRv': 0.0019304769537242656,
'NIRv_1': -0.01618882994732882,
'NIRv_2': 0.00825563495036525,
'NIRv_3': 0.00981020972287458,
'NIRv_4': -0.015757174839531186,
'NIRv_5': -0.015470577135366363,
'SPCAI': -0.006914922177500003,
'SPCAI_1': -0.02156087575249999,
'SPCAI_2': -0.0012689877900000223,
'SPCAI_3': -0.009334751402500004,
'SPCAI_4': -0.011782810367500026,
'SPCAI_5': -0.015560948357499993,
'landcover': 2}}]
(实际上有1000多个字典再数组里,这里值取了前三个)
我想按照它的landcover的值自动对这个数组分类,landcover = 3是一类,landcover = 2是一类
有没有不用for循环的方法,for循环太慢了。
希望有大佬赐教
sample = [{'type': 'Feature',
'geometry': {'type': 'Point',
'coordinates': [121.57870893028795, 40.832029370655775]},
'id': '0',
'properties': {'landcover': 3}},
{'type': 'Feature',
'geometry': {'type': 'Point',
'coordinates': [121.84930925332695, 40.808922548991774]},
'id': '1',
'properties': {'landcover': 1}},
{'type': 'Feature',
'geometry': {'type': 'Point',
'coordinates': [121.73990903059295, 40.826274218052355]},
'id': '5',
'properties': {'landcover': 3}},
{'type': 'Feature',
'geometry': {'type': 'Point',
'coordinates': [121.70545607685489, 40.820899158859554]},
'id': '6',
'properties': {'landcover': 3}},
{'type': 'Feature',
'geometry': {'type': 'Point',
'coordinates': [121.56575598071325, 40.84394301362967]},
'id': '7',
'properties': {'landcover': 3}},
{'type': 'Feature',
'geometry': {'type': 'Point',
'coordinates': [121.62382650556756, 40.82640435267018]},
'id': '9',
'properties': {'landcover': 3}},
{'type': 'Feature',
'geometry': {'type': 'Point',
'coordinates': [121.62346749323697, 40.83227011445792]},
'id': '10',
'properties': {'landcover': 3}},
{'type': 'Feature',
'geometry': {'type': 'Point',
'coordinates': [121.69225946089837, 40.822774377925136]},
'id': '12',
'properties': {'landcover': 3}}]
l1, l2, l3 = [], [], []
for i in sample:
if i['properties']['landcover'] == 1:
l1.append(i)
elif i['properties']['landcover'] == 2:
l2.append(i)
elif i['properties']['landcover'] == 3:
l3.append(i)
print(l1, l2, l3, sep="\n\n")
|
|