Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:如何将for循环结果存储在列表中?_Python_Arrays_List_Loops_For Loop - Fatal编程技术网

Python:如何将for循环结果存储在列表中?

Python:如何将for循环结果存储在列表中?,python,arrays,list,loops,for-loop,Python,Arrays,List,Loops,For Loop,我的输出如下,这是循环迭代的结果。基本上,下面的代码是使用交集和并集计算来比较不同大小的数组。 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 re

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

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]

有人能帮我吗?

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

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

在这行代码中,您在这里显式地将值定义为float。或者,您可以使用map()函数和split()sim变量。

那么循环似乎就是这个(在代码末尾):

一种可能的解决办法是写:

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

你能展示产生这些结果的循环吗?您可以使用
list.append()
或列表理解。当我尝试list.append()时,它返回此错误。TypeError:描述符“append”需要“list”对象,但第二次收到“float”,请向我们显示循环。我们无法通过猜测AlonTypeError来帮助您:“float”对象不可编辑。我收到了这个错误,因为我不能把它放在列表(一个)感谢回复的地方。这真的很有用。最后两行可以是:output.append(一次)打印输出
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