【问题标题】:Python: How to store for loop result in a list?Python:如何将 for 循环结果存储在列表中?
【发布时间】:2015-08-24 12:11:44
【问题描述】:

我的输出如下,这是循环迭代的结果。下面的代码基本上是使用交集和联合计算来比较不同大小的数组。:

import numpy as py
import itertools
from itertools import izip
def diffs(a,b):
# collect sliding window differences
# length of window determined by the shorter array
# if a,b are not arrays, need to replace b[...]-a with
# a list comprehension
    n,m=len(a),len(b)
    if n>m:
    # ensure s is the shorter
        b,a=a,b # switch
        n,m=len(a),len(b)
    # may need to correct for sign switch
    result=[]
    for i in range(0,1+m-n):
        result.append(b[i:i+n]-a)
    return result

###################################################################

def alldiffs(a,b):
# collect all the differences for elements of a and b
# a,b could be lists or arrays of arrays, or 2d arrays
    result=[]
    for aa in a:
        for bb in b:
            result.append(diffs(aa,bb))
    return result

###################################################################

def count_total(a,b):
#count the total number of element for two arrays in different list
#return [sum(map(len, i)) for i in product(a, b)]
    y= lambda x:len(x)
    result=[]
    for a1 in a:
        for b1 in b:
            result.append(y(a1) + y(b1))
    return result

##################################################################

def count_zero(obj):
#count the total number of zero for two arrays in different list
    if isinstance(obj,list):
        return list(map(count_zero,obj))
    else:
        return Counter(obj)[0]

a=[np.array([2,2,1,2]),np.array([1,3])]
b=[np.array([4,2,1])]
c=[np.array([1,2]),np.array([4,3])]

for i,j in itertools.combinations([a,b,c],2):
    all_diffs=alldiffs(i,j)
    total=count_total(i,j)
    zero=count_zero(all_diffs)
    total=np.array(total)
    union=map(sub,total,zero)
    zero=np.array(zero).tolist()
    union=np.array(union).tolist()
    union=[list(x) for x in union]  
    sim=[[float(aaa) / bbb for (aaa, bbb) in itertools.izip(aa, bb)] \
        for (aa, bb) in itertools.izip(zero, union)]
    sim_comb=sum(sim,[])
    sum_of_sim=sum(sim_comb)
    number_sum=len(sim_comb)
    ave=sum_of_sim/number_sum
    one_ave=1-ave
    print one_ave

输出

>>> 
0.9
0.829166666667
0.875

如何将它们保存到列表中,以便我的输出如下:

>>>
[0.9,0.829166666667,0.875]

谁能帮帮我?

【问题讨论】:

  • 你能展示产生这些结果的循环吗?您可以使用list.append() 或列表理解。
  • 当我尝试 list.append() 时返回此错误。类型错误:描述符“附加”需要一个“列表”对象但收到一个“浮点数”
  • 第二次,请给我们看循环。我们无法通过单独猜测来帮助您

标签: python arrays list loops for-loop


【解决方案1】:

正如您的评论所建议的,您需要将它们转换为列表项而不是浮点数。

sim=[[float(aaa) / bbb for (aaa, bbb) in itertools.izip(aa, bb)] \ 
    for (aa, bb) in itertools.izip(zero, union)] 

在这行代码中,您在此处明确将值定义为浮点数。或者,您可以使用 map() 函数和 split() sim 变量。

【讨论】:

  • TypeError: 'float' 对象不可迭代。我收到此错误是因为我不能以这种方式将它放在 list(one_ave)
  • 感谢您的回复。真的很有帮助。
【解决方案2】:

循环似乎是这个(在代码的末尾):

for i,j in itertools.combinations([a,b,c],2):
    all_diffs=alldiffs(i,j)
    total=count_total(i,j)
    zero=count_zero(all_diffs)
    total=np.array(total)
    union=map(sub,total,zero)
    zero=np.array(zero).tolist()
    union=np.array(union).tolist()
    union=[list(x) for x in union]  
    sim=[[float(aaa) / bbb for (aaa, bbb) in itertools.izip(aa, bb)] \
        for (aa, bb) in itertools.izip(zero, union)]
    sim_comb=sum(sim,[])
    sum_of_sim=sum(sim_comb)
    number_sum=len(sim_comb)
    ave=sum_of_sim/number_sum
    one_ave=1-ave
    print one_ave

一种可能的解决方案是这样写:

output = []

for i,j in itertools.combinations([a,b,c],2):
    all_diffs=alldiffs(i,j)
    total=count_total(i,j)
    zero=count_zero(all_diffs)
    total=np.array(total)
    union=map(sub,total,zero)
    zero=np.array(zero).tolist()
    union=np.array(union).tolist()
    union=[list(x) for x in union]  
    sim=[[float(aaa) / bbb for (aaa, bbb) in itertools.izip(aa, bb)] \
        for (aa, bb) in itertools.izip(zero, union)]
    sim_comb=sum(sim,[])
    sum_of_sim=sum(sim_comb)
    number_sum=len(sim_comb)
    ave=sum_of_sim/number_sum
    one_ave=1-ave
    output += [one_ave]

print output

【讨论】:

  • 最后两行可能是:output.append(one_ave) 打印输出
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多