2010-08-11 90 views
1

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

当我使用类似的东西:

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

不好意思,格式化是有点棘手......第二个循环是第一位的内部... 我认为它比较的第一个条目列表,所以它是一样的...所以我必须在第二个条目的第二个循环中开始,但是我不能用i-1来做,因为我是空洞字典... 有人想法吗? 在此先感谢!

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 

好吧,我想......此刻那是更加令人困惑:)...

+0

你在用什么语言? – Jonn 2010-08-11 12:42:13

+0

我正在使用python – aleho 2010-08-11 12:43:40

+0

如果三个或更多元素具有相同的“id”,会发生什么?你想计算每对元素的平均值吗?或者你是否想用相同的''id''来聚类所有的元素并且取平均值? – unutbu 2010-08-11 12:46:20

回答

4

一个办法是改变你存储你的观点的方式,因为你已经注意到了,这是很难得到你想要的东西。

一个更为有用的结构将是一个字典,其中id映射到点的列表:

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]) 
+0

由于THC4k是正确的,我收回了我正处于写作过程中的评论。一张指令清单根本不理想 - 更自然地,您有一个字典,您可以先将其删除。 +1 但是,THC4k,如果你在代码中加入了如何从列表中构建这样一个字典,以便使OP更清晰,那将会很不错。 – chryss 2010-08-11 13:08:08

+0

所以,当我去与points_dict [id] .append(...)我得到一个字典与几个项目与不同IDS和几何? 我想这样做与词典的列表,因为我找不到任何东西存储在一个字典中的几个项目(如在Excel表中)... – aleho 2010-08-11 13:14:06

+2

通知'defaultdict(列表)'使用内置'列表'这就是为什么你应该从不**使用'list'作为变量名称 – 2010-08-11 13:17:18

0

我不能完全确定你想要做什么,但我想过滤列表会帮助你。有内置函数filter,它对一个序列进行迭代,并为每个项目调用用户定义的函数,以确定是否将该项目包含在结果列表中。

例如:

def is4(number): 
    return number == 4 

l = [1, 2, 3, 4, 5, 6, 4, 7, 8, 4, 4] 
filter(is4, l) # returns [4, 4, 4, 4] 

因此,拥有一个字典列表,过滤掉某些条目等于给定值的所有词典,你可以做这样的事情:

def filter_dicts(dicts, entry, value): 
    def filter_function(d): 
     if entry not in d: 
     return False 
     return d[entry] == value 
    return filter(filter_function, dicts) 

使用此功能,要获得所有带有“id”条目的字典等于2,您可以执行:

result = filter_dicts(your_list, "id", 2) 

有了这个,你的主循环可能会是这个样子:

processed_ids = set() 
for item in list: 
    id = item['id'] 
    if id in processed_ids: 
     continue 
    processed_ids.add(id) 
    same_ids = filter_dicts(list, "id", id) 
    # now do something with same_ids 

我希望我理解正确的,你并认为这是对您有所帮助。

+0

哦,太棒了!非常感谢...看起来不错...我必须再次阅读并与我的尝试! – aleho 2010-08-11 13:31:27

+0

它的工作!jipie! – aleho 2010-08-11 14:11:26