wlp1818100227 发表于 2022-3-8 16:41:14

不用for循环,对嵌套在数组内的字典根据key的值进行分类

有这样一个嵌套字典的数组:

[{'type': 'Feature',
'geometry': {'type': 'Point',
   'coordinates': },
'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': },
'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': },
'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循环太慢了。
希望有大佬赐教

qq1151985918 发表于 2022-3-8 16:41:15

sample =[{'type': 'Feature',
'geometry': {'type': 'Point',
   'coordinates': },
'id': '0',
'properties': {'landcover': 3}},
{'type': 'Feature',
'geometry': {'type': 'Point',
   'coordinates': },
'id': '1',
'properties': {'landcover': 1}},
{'type': 'Feature',
'geometry': {'type': 'Point',
   'coordinates': },
'id': '5',
'properties': {'landcover': 3}},
{'type': 'Feature',
'geometry': {'type': 'Point',
   'coordinates': },
'id': '6',
'properties': {'landcover': 3}},
{'type': 'Feature',
'geometry': {'type': 'Point',
   'coordinates': },
'id': '7',
'properties': {'landcover': 3}},
{'type': 'Feature',
'geometry': {'type': 'Point',
   'coordinates': },
'id': '9',
'properties': {'landcover': 3}},
{'type': 'Feature',
'geometry': {'type': 'Point',
   'coordinates': },
'id': '10',
'properties': {'landcover': 3}},
{'type': 'Feature',
'geometry': {'type': 'Point',
   'coordinates': },
'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")

wp231957 发表于 2022-3-8 16:50:42

1000个数据,不会狠慢吧

qq1151985918 发表于 2022-3-8 16:51:47

循环肯定是要用的,但是我看了下这数据,即使是循环也绝对不会慢,很简单的,是不是你代码写的效率低了,不妨把你的代码和数据发上来,我给你试一下,感觉挺简单的,效率绝对不是你想的那么慢,按你说的1000个字典我感觉几秒钟就完事了

wlp1818100227 发表于 2022-3-8 17:09:03

qq1151985918 发表于 2022-3-8 16:51
循环肯定是要用的,但是我看了下这数据,即使是循环也绝对不会慢,很简单的,是不是你代码写的效率低了,不 ...

#精简了一下数据选了前面几个

sample =[{'type': 'Feature',
'geometry': {'type': 'Point',
   'coordinates': },
'id': '0',
'properties': {'landcover': 3}},
{'type': 'Feature',
'geometry': {'type': 'Point',
   'coordinates': },
'id': '1',
'properties': {,'landcover': 1}},
{'type': 'Feature',
'geometry': {'type': 'Point',
   'coordinates': },
'id': '5',
'properties': {'landcover': 3}},
{'type': 'Feature',
'geometry': {'type': 'Point',
   'coordinates': },
'id': '6',
'properties': {'landcover': 3}},
{'type': 'Feature',
'geometry': {'type': 'Point',
   'coordinates': },
'id': '7',
'properties': {'landcover': 3}},
{'type': 'Feature',
'geometry': {'type': 'Point',
   'coordinates': },
'id': '9',
'properties': {'landcover': 3}},
{'type': 'Feature',
'geometry': {'type': 'Point',
   'coordinates': },
'id': '10',
'properties': {'landcover': 3}},
{'type': 'Feature',
'geometry': {'type': 'Point',
   'coordinates': },
'id': '12',
'properties': {'landcover': 3}}]

count = len(sample)
landcover = [[]for i in range(count)]

#把landcover的值放入数组中
for index in range(0,count):
    land = featureinfo.getInfo()
    landcover = land['properties']['landcover']

#把landcover三个值的长度提取出来
idx =
length_PA = len(idx)
print(len(idx))
idx2 =
length_SS = len(idx2)
print(len(idx2))
idx3 =
length_flat = len(idx3)
print(len(idx3))

#创建三个对应的数组
PA = [[]for i in range(length_PA)]
SS = [[]for i in range(length_SS)]
flat = [[]for i in range(length_flat)]

#用for循环分类
for index in range(0,count):
    ss = featureinfo.get('landcover').getInfo()
    print(ss)
    if ss == 2:
      SS = featureinfo
      i = i+1
    elif ss == 1:
      PA = featureinfo
      j = j+1
    elif ss == 3:
      flat = featureinfo
      k = k+1

这样很慢

wlp1818100227 发表于 2022-3-8 18:14:46

qq1151985918 发表于 2022-3-8 16:41


啊,得出结果了,非常感谢

wlp1818100227 发表于 2022-3-9 08:24:17

qq1151985918 发表于 2022-3-8 16:41


大佬,请问一下,这个原理是什么呢,是因为这个append函数增加数组的元素的速度比较快吗

qq1151985918 发表于 2022-3-9 09:49:41

wlp1818100227 发表于 2022-3-9 08:24
大佬,请问一下,这个原理是什么呢,是因为这个append函数增加数组的元素的速度比较快吗

因为你写的代码我看不懂,就算看得懂也绝对是舍本逐末,不说一定是错误的,但肯定不正确。而我写的就是正常的,正常代码就应该是我这样的。根据你带我我认为你知识学的还不透彻,建议再学习一下字典的用法,字典学会了就理解了。我那个代码很简洁很简单也很好理解。

qq1151985918 发表于 2022-3-9 09:50:36

跟列表跟append没关系,是字典方面的知识你理解的不到位。
页: [1]
查看完整版本: 不用for循环,对嵌套在数组内的字典根据key的值进行分类