[英]Averaging between select varibles on an RLE list, having trouble with the last element
因此,我使用游程长度编码创建了一个压缩列表。 现在,我试图在列表中的某些变量(例如450
和180
)中找到平均值。 该代码应该像这样工作
a=[[180,3],[140,1],[160,1],[150,2],[450,1][180,4]]
print mean(a)
>[[180,3],[150,4],[450,1][180,4]]
我对此很陌生,否则我将在压缩过程中执行平均。
我所坚持的是两件事:未压缩时得到的结果列表与原始列表的长度不相同,并且我不确定如果代码不通过最后一个元素,如何追加最后一个元素。 我可以在for循环中使用诸如elif i[0].index==len(lst)
类的索引,但这在计算上会非常昂贵(数据集相当大)。 我创建的是for循环外的最终if语句,但是结果列表的长度仍然与原始长度不同。
def mean(lst):
sm=0
count=0
new=[]
for i in lst:
if i[0] is None:
new.append([0,1])
elif i[0]!=180.0 and i[0]!=450.0:
sm+=(i[0]*i[1])
count+=i[1]
elif count==0:
new.append(i)
else:
new.append([sm/count,count])
new.append(i)
count=0
sm=0
if count>0:
new.append([sm/count,count])
pass
return (new)
对于那些以后会研究该问题的人,我添加了将压缩和平均相结合的解决方案。 为了阐明目的,我正在GIS程序中压缩路段之间的角度以创建较小的数据集。 450可以视为空值。
with arcpy.da.SearchCursor("test_loop",["angle3"]) as cursor:
count1 = 0
count2=0
count3=0
add=0
lst=[]
for row in cursor:
if row[0]<180 and row[0] is not None:
if count1>0:
lst.append([180,count1+count3])
count1=0
count3=0
pass
count2+=1
add+=row[0]
elif row[0]==180:
if count2>0:
lst.append([add/count2,count2+count3])
count2=0
count3=0
add=0
pass
count1+=1
elif row[0]==450 or row[0] is None:
count3+=1
else:
print "Error"
break
if count1>0:
lst.append([180,count1+count3])
count1=0
count3=0
elif count2>0:
lst.append([add/count2,count2+count3])
count2=0
count3=0
add=0
else:
lst.append([None,count3])
print lst
del cursor
del row
def decode(lst):
q = []
for i in lst:
for x in range(i[1]):
q.append (i[0])
return q
final=decode(lst)
print final
with arcpy.da.UpdateCursor("test_loop",["curve_level"]) as cursor:
i=0
for row in cursor:
row[0]=final[i]
i+=1
cursor.updateRow(row)
del cursor
del row
假设您的输出中不应有重复的条目180,并且您的预期输出是:
[[180,7],[150,4],[450,1]]
我认为这可以做到:
from collections import defaultdict
def mean(lst):
d = defaultdict(int)
sm, count = 0.0, 0
for [k, v] in lst:
if float(k) in [180.0,450.0]:
d[k] += v
else:
sm += k*v
count +=v
if sm != 0: d[sm/count] = count
return [list(itm) for itm in d.items()]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.