鱼C论坛

 找回密码
 立即注册
查看: 1082|回复: 8

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

[复制链接]
发表于 2022-3-8 16:41:14 | 显示全部楼层 |阅读模式
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循环太慢了。
希望有大佬赐教
最佳答案
2022-3-8 16:41:15
  1. sample =  [{'type': 'Feature',
  2.   'geometry': {'type': 'Point',
  3.    'coordinates': [121.57870893028795, 40.832029370655775]},
  4.   'id': '0',
  5.   'properties': {'landcover': 3}},
  6. {'type': 'Feature',
  7.   'geometry': {'type': 'Point',
  8.    'coordinates': [121.84930925332695, 40.808922548991774]},
  9.   'id': '1',
  10.   'properties': {'landcover': 1}},
  11. {'type': 'Feature',
  12.   'geometry': {'type': 'Point',
  13.    'coordinates': [121.73990903059295, 40.826274218052355]},
  14.   'id': '5',
  15.   'properties': {'landcover': 3}},
  16. {'type': 'Feature',
  17.   'geometry': {'type': 'Point',
  18.    'coordinates': [121.70545607685489, 40.820899158859554]},
  19.   'id': '6',
  20.   'properties': {'landcover': 3}},
  21. {'type': 'Feature',
  22.   'geometry': {'type': 'Point',
  23.    'coordinates': [121.56575598071325, 40.84394301362967]},
  24.   'id': '7',
  25.   'properties': {'landcover': 3}},
  26. {'type': 'Feature',
  27.   'geometry': {'type': 'Point',
  28.    'coordinates': [121.62382650556756, 40.82640435267018]},
  29.   'id': '9',
  30.   'properties': {'landcover': 3}},
  31. {'type': 'Feature',
  32.   'geometry': {'type': 'Point',
  33.    'coordinates': [121.62346749323697, 40.83227011445792]},
  34.   'id': '10',
  35.   'properties': {'landcover': 3}},
  36. {'type': 'Feature',
  37.   'geometry': {'type': 'Point',
  38.    'coordinates': [121.69225946089837, 40.822774377925136]},
  39.   'id': '12',
  40.   'properties': {'landcover': 3}}]

  41. l1, l2, l3 = [], [], []
  42. for i in sample:
  43.     if i['properties']['landcover'] == 1:
  44.         l1.append(i)
  45.     elif i['properties']['landcover'] == 2:
  46.         l2.append(i)
  47.     elif i['properties']['landcover'] == 3:
  48.         l3.append(i)
  49. print(l1, l2, l3, sep="\n\n")
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-3-8 16:41:15 | 显示全部楼层    本楼为最佳答案   
  1. sample =  [{'type': 'Feature',
  2.   'geometry': {'type': 'Point',
  3.    'coordinates': [121.57870893028795, 40.832029370655775]},
  4.   'id': '0',
  5.   'properties': {'landcover': 3}},
  6. {'type': 'Feature',
  7.   'geometry': {'type': 'Point',
  8.    'coordinates': [121.84930925332695, 40.808922548991774]},
  9.   'id': '1',
  10.   'properties': {'landcover': 1}},
  11. {'type': 'Feature',
  12.   'geometry': {'type': 'Point',
  13.    'coordinates': [121.73990903059295, 40.826274218052355]},
  14.   'id': '5',
  15.   'properties': {'landcover': 3}},
  16. {'type': 'Feature',
  17.   'geometry': {'type': 'Point',
  18.    'coordinates': [121.70545607685489, 40.820899158859554]},
  19.   'id': '6',
  20.   'properties': {'landcover': 3}},
  21. {'type': 'Feature',
  22.   'geometry': {'type': 'Point',
  23.    'coordinates': [121.56575598071325, 40.84394301362967]},
  24.   'id': '7',
  25.   'properties': {'landcover': 3}},
  26. {'type': 'Feature',
  27.   'geometry': {'type': 'Point',
  28.    'coordinates': [121.62382650556756, 40.82640435267018]},
  29.   'id': '9',
  30.   'properties': {'landcover': 3}},
  31. {'type': 'Feature',
  32.   'geometry': {'type': 'Point',
  33.    'coordinates': [121.62346749323697, 40.83227011445792]},
  34.   'id': '10',
  35.   'properties': {'landcover': 3}},
  36. {'type': 'Feature',
  37.   'geometry': {'type': 'Point',
  38.    'coordinates': [121.69225946089837, 40.822774377925136]},
  39.   'id': '12',
  40.   'properties': {'landcover': 3}}]

  41. l1, l2, l3 = [], [], []
  42. for i in sample:
  43.     if i['properties']['landcover'] == 1:
  44.         l1.append(i)
  45.     elif i['properties']['landcover'] == 2:
  46.         l2.append(i)
  47.     elif i['properties']['landcover'] == 3:
  48.         l3.append(i)
  49. print(l1, l2, l3, sep="\n\n")
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-3-8 16:50:42 From FishC Mobile | 显示全部楼层
1000个数据,不会狠慢吧
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-3-8 16:51:47 | 显示全部楼层
循环肯定是要用的,但是我看了下这数据,即使是循环也绝对不会慢,很简单的,是不是你代码写的效率低了,不妨把你的代码和数据发上来,我给你试一下,感觉挺简单的,效率绝对不是你想的那么慢,按你说的1000个字典我感觉几秒钟就完事了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

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

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}}]

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

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

#把landcover三个值的长度提取出来
idx = [idx for (idx, val) in enumerate(landcover) if val == 1]
length_PA = len(idx)
print(len(idx))
idx2 = [idx2 for (idx2, val) in enumerate(landcover) if val == 2]
length_SS = len(idx2)
print(len(idx2))
idx3 = [idx3 for (idx3, val) in enumerate(landcover) if val == 3]
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[index].get('landcover').getInfo()
    print(ss)
    if ss == 2:
        SS = featureinfo[index]
        i = i+1
    elif ss == 1:
        PA[j] = featureinfo[index]
        j = j+1
    elif ss == 3:
        flat[k] = featureinfo[index]
        k = k+1

这样很慢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-3-8 18:14:46 | 显示全部楼层

啊,得出结果了,非常感谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-3-9 08:24:17 | 显示全部楼层

大佬,请问一下,这个原理是什么呢,是因为这个append函数增加数组的元素的速度比较快吗
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-3-9 09:49:41 From FishC Mobile | 显示全部楼层
wlp1818100227 发表于 2022-3-9 08:24
大佬,请问一下,这个原理是什么呢,是因为这个append函数增加数组的元素的速度比较快吗

因为你写的代码我看不懂,就算看得懂也绝对是舍本逐末,不说一定是错误的,但肯定不正确。而我写的就是正常的,正常代码就应该是我这样的。根据你带我我认为你知识学的还不透彻,建议再学习一下字典的用法,字典学会了就理解了。我那个代码很简洁很简单也很好理解。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-3-9 09:50:36 From FishC Mobile | 显示全部楼层
跟列表跟append没关系,是字典方面的知识你理解的不到位。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-22 08:02

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表