-6

I have my output as below which is the result of iteration over looping. Basically the code below is to compare arrays of different sizes using intersection and union calculation.:

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

Output

>>> 
0.9
0.829166666667
0.875

How can I save them into list so my output will be like:

>>>
[0.9,0.829166666667,0.875]

Can anyone help me?

Xiong89
  • 767
  • 2
  • 13
  • 24
  • 3
    Can you show the loop that is producing those results? You can either use `list.append()` or a list comprehension. – Cory Kramer Jun 09 '15 at 15:12
  • It return this error when i tried list.append(). TypeError: descriptor 'append' requires a 'list' object but received a 'float' – Xiong89 Jun 09 '15 at 15:15
  • 3
    For the second time, please show us the loop. We cannot help you by guessing alone – Cory Kramer Jun 09 '15 at 15:15

2 Answers2

1

Well the loop seems to be this one (at the end of the code):

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

One possible solution would be to write:

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
zezollo
  • 4,606
  • 5
  • 28
  • 59
0

As your comment suggested you need to convert them to a list items instead of floats.

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

In this line of code you're explicitly defining the values as float in here. Alternatively you can use use the map() function and split() the sim variable.

Leb
  • 15,483
  • 10
  • 56
  • 75