2013-04-20 93 views
0

我生产从3个值之差的平均值,并希望把它放在一个列表检查,如果值已经在列表中

列表中的示例中,我想平均看起来像这样:

... 
[6.0, 270.0, -55.845848680633168], 
[6.0, 315.0, -47.572000492889323], 
[6.5, 0.0, -47.806802767243724], 
[6.5, 45.0, -48.511643275159528], 
[6.5, 90.0, -45.002053150122123], 
[6.5, 135.0, -51.034656702050455], 
[6.5, 180.0, -53.266356523649002], 
[6.5, 225.0, -47.872632929518339], 
[6.5, 270.0, -52.09662072002746], 
[6.5, 315.0, -48.563996448937075]] 

前两列匹配时会有多达3行(这两列是极坐标),当这种情况下,我想取第三个元素之间的差异,求平均值并追加极性点的坐标和平均结果放入新列表中

for a in avg_data: 
    comparison = [] 
    for b in avg_data: 
     if a[0] == b[0] and a[1] == b[1]: 
      comparison.append(b[2]) 

    print comparison  
    z = 0 # reset z to 0, z does not need set now in if len(comp) == 1 

    if len(comparison) == 2: # if there are only 2 elements, compare them 
     z += -(comparison[0]) + comparison[1] 
    if len(comparison) == 3: # if all 3 elements are there, compare all 3 
     z += -(comparison[0]) + comparison[1] 
     z += -(comparison[0]) + comparison[2] 
     z += -(comparison[1]) + comparison[2] 
     z = z/3 #average the variation 

    avg_variation.append([a[0], a[1], z]) #append the polar coordinates and the averaged variation to a list 

此代码将正确的数据输出到列表中,除了每次遇到匹配的极坐标时输出该数据,因此最终会出现重复的行。

要停止这个我试过执行if语句来寻找执行平均再

if a[0] not in avg_variation and a[1] not in avg_variation: 

这不起作用之前在avg_variation名单极坐标匹配,我得到的错误

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() 

我不认为任何或全部是我正在寻找的,因为我只想检查前两列而不是第三列对已经附加的值。任何人有任何想法我怎么能使我的陈述更好?

要清理多一点我的实际问题是什么:

我的代码通过对列表嵌套的列表,其中1号2个单元匹配搜索,执行3号元素的计算,然后将它们附加到一个新的列表。我的问题是,如果有2或3行的前2个元素相匹配,它将结果附加到新列表中2或3次,我希望它只做一次

编辑:对不起,我最初的问题是misleadng关于我的代码的目的。

+0

所以,只是为了得到它直,你得到的名单都应该在他们的第三个数量方面相同的值(这应该是你的第三列的平均值以上)? – arshajii 2013-04-20 17:36:55

+0

结果列表应该有嵌套列表,其第一个和第二个元素是极坐标,第三个元素是这些极坐标值的平均值。在结果列表中,每行应该有唯一的极坐标。 – 2013-04-20 17:49:52

+0

@markmcmurray:你的代码并不计算这些值的平均值,但它会计算出不同元素之间的平均(有符号)差异。 – DSM 2013-04-20 17:52:00

回答

3

IIUC,我认为一个简单的方法是像

import numpy as np 
from itertools import combinations 
from collections import defaultdict 

def average_difference(seq): 
    return np.mean([j-i for i,j in combinations(seq, 2)]) if len(seq) > 1 else 0 

def average_over_xy(seq, fn_to_apply): 
    d = defaultdict(list) 
    for x,y,z in seq: 
     d[x,y].append(z) 

    outlist = [[x,y,fn_to_apply(z)] for (x,y),z in sorted(d.items())] 
    return outlist 

它遍历所有行,使一本字典,其中x,y坐标是键和元素的值列表,然后将该字典转换为列表的排序列表,在z中的元素中应用指定的功能。例如,我们可以在代码中使用的平均签约,并下令区别,如:

产生

>>> seq = [[1, 2, 30], [1, 2, 40], [1, 2, 50], [1, 3, 4], [1, 3, 6], [2, 10, 5]] 
>>> average_over_xy(seq, average_difference) 
[[1, 2, 13.333333333333334], [1, 3, 2.0], [2, 10, 0]] 

请注意,这我上面匹配,答案您已经定义的方式,取决于元素的顺序,即

>>> average_over_xy([[1,2,3],[1,2,4]], average_difference) 
[[1, 2, 1.0]] 
>>> average_over_xy([[1,2,4],[1,2,3]], average_difference) 
[[1, 2, -1.0]] 

如果你愿意,你可以使用

def average_difference_sorted(seq): 
    return average_difference(sorted(seq)) 

代替或使用标准偏差或任何你喜欢的。 (你没有提到你的用例,所以我会假设你已经按照你想要的顺序得到了这个列表,你知道这个缺陷,而你真的需要average_difference)。

基于numpy的一些技巧我们可以做到,并且有一些推广的方法,但是使用defaultdict来累积值是一个方便的模式,它通常足够快。

+0

这工作,谢谢。对不起,以一种令人困惑的方式来说明问题。 – 2013-04-20 18:14:02

0

你还没有给出所有必要的信息来确定这一点,但我相信你的错误是由对numpy数组执行逻辑操作引起的。请参阅this answer以解决类似错误的问题。

没有更多的信息,很难复制问题的上下文来尝试它,但可能在if语句的布尔操作中更具体。

1

这里是一个可能的解决方案:

l=[[6.0, 270.0, -55.845848680633168], 
[6.0, 315.0, -47.572000492889323], 
[6.5, 0.0, -47.806802767243724], 
[6.0, 180.0, -53.266356523649002], 
[6.0, 225.0, -47.872632929518339], 
[6.0, 270.0, -52.09662072002746], 
[6.0, 315.0, -48.563996448937075]] 

# First, we change the structure so that the pair of coordinates 
# becomes a tuple which can be used as dictionary key 
l=[[(c1, c2), val] for c1, c2, val in l] 

# We build a dictionary coord:[...list of values...] 
d={} 
for coord, val in l: 
    d.setdefault(coord,[]).append(val) 

# Here, I compute the mean of each list of values. 
# Apply your own function ! 

means = [[coord[0], coord[1], sum(vals)/len(vals)] for coord, vals in d.items()] 

print means 
相关问题