循环通过字典列表 [英] loop through list of dictionaries

查看:128
本文介绍了循环通过字典列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字典列表。列表中有几个点,一些是多个。当有多个条目时,我想计算这一点的x和y的平均值。我的问题是,我不知道如何循环通过字典列表来比较ids的点!



当我使用这样的东西:

 为列表中的列表:
为列表中的j:
如果我['id'] == j ['id']:
point = getPoint(i ['geom'])
....

对不起,形成有点棘手...第二个循环是在第一个...
i认为它比较列表的第一个条目,所以是一样的。所以我必须从第二个循环开始,第二个条目,但我不能这样做与i-1,因为我是孔字典...
有人有个想法?
感谢提前!

 为范围内的j(1,len(NEWPoint)))
if i ['gid'] == j ['gid']:
allsamePoints.append(j)
for all inamePoints:
for l in range(1,len(allsamePoints)) :
如果k ['gid'] == l ['gid']:
Point1 = k ['geom']
Point2 = l ['geom']
X =(Point1.x()+ Point2.x())/ 2
Y =(Point1.y()+ Point2.y())/ 2
AVPoint = QgsPoint(X,Y)
NEWReturnList.append({'gid':j ['gid'],'geom':AVPoint})
del l
在NEWReturnList中的m:
对于范围内的n( 1,len(NEWReturnList)):
如果m ['gid'] == n ['gid']:
Point1 = m ['geom']
Point2 = n ['geom ']
X =(Point1.x()+ Point2.x())/ 2
Y =(Point1.y()+ Point2.y())/ 2
AV Point = QgsPoint(X,Y)
NEWReturnList.append({'gid':j ['gid'],'geom':AVPoint})
del n
else:
pass

好的,我想...在这个更令人困惑的时刻:)...

解决方案

一种方式将改变你存储积分的方式,因为你已经注意到,很难得到你想要的东西的。



一个更有用的结构将是一个dict,其中<​​code> id 映射到一个列表:

 从集合导入defaultdict 
points_dict = defaultdict(list)

#使新的dict
for point in point_list:
id = point [id]
points_dict [id] .append(point ['geom'])

def avg(lst) :
lst的平均值
返回1.0 * sum(lst)/ len(lst)

#现在简单得到平均
for point in points_dict:
print id,avg(points_dict [id])


i have a list of dictionaries. there are several points inside the list, some are multiple. When there is a multiple entry i want to calculate the average of the x and the y of this point. My problem is, that i don't know how to loop through the list of dictionaries to compare the ids of the points!

when i use something like that:

for i in list:
  for j in list:
    if i['id'] == j['id']:
      point = getPoint(i['geom'])
      ....

sorry, the formating is a little bit tricky... the second loop is inside the first one... i think it compares the first entry of the list, so it's the same... so i have to start in the second loop with the second entry, but i can't do that with i-1 because i is the hole dictionary... Someone an idea? thanks in advance!

 for j in range(1, len(NEWPoint)):
      if i['gid']==j['gid']:
         allsamePoints.append(j)
      for k in allsamePoints:
         for l in range(1, len(allsamePoints)):
            if k['gid']==l['gid']:
                Point1 = k['geom']
                Point2=l['geom']
                X=(Point1.x()+Point2.x())/2
                Y=(Point1.y()+Point2.y())/2
                AVPoint = QgsPoint(X, Y)
                NEWReturnList.append({'gid': j['gid'], 'geom': AVPoint})
                del l
      for m in NEWReturnList:
          for n in range(1, len(NEWReturnList)):
              if m['gid']==n['gid']:
                 Point1 = m['geom']
                 Point2=n['geom']
                 X=(Point1.x()+Point2.x())/2
                 Y=(Point1.y()+Point2.y())/2
                 AVPoint = QgsPoint(X, Y)
                 NEWReturnList.append({'gid': j['gid'], 'geom': AVPoint})
                 del n
              else:
                 pass

ok, i think... at the moment thats more confusing :)...

解决方案

One way would be changing the way you store your points, because as you already noticed, it's hard to get what you want out of it.

A much more useful structure would be a dict where the id maps to a list of points:

from collections import defaultdict
points_dict = defaultdict(list)

# make the new dict
for point in point_list:
    id = point["id"]
    points_dict[id].append(point['geom'])

def avg( lst ):
    """ average of a `lst` """
    return 1.0 * sum(lst)/len(lst)

# now its simple to get the average
for id in points_dict:
    print id, avg( points_dict[id] )

这篇关于循环通过字典列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆