Biotechgeek - 1 year ago 120
Python Question

# How to divide an integer by a list in Python?

``````sample = [['AAAA','BBBB','CCCC'],['BBBBB','FFFFF','GGGGG'],['AA','MM']]
``````

I need to calculate 'a' such that a = summation 1/i; where i ranges from 1 to n-1. In the process, I need to divide an integer (MyInt) by a list.

``````i2 =[]
afinal =[]
for sub_list in sample:
i1 = range(len(sample[0]))
i1.pop(0)
myInt = [1 for x in i1]
newList = [float(int1)/float(x) for int1,x in zip(myInt,i1)]
a = [sum(i) for i in zip(newList)]
afinal.append(a)
print afinal
``````

However, I get the output as [[1.0]], whereas I should be getting an output with as [1.83333333333, 2.08333333333,1] numbers within a list.

Any idea where I may be going wrong?

I need to calculate 'a' such that a = summation 1/i; where i ranges from 1 to n-1

``````>>> n = 5
>>> a = sum(1.0 / i for i in range(1,n))
>>> a
2.083333333333333
>>> 1./1 + 1./2 + 1./3 + 1./4
2.083333333333333
``````

Is that what you are trying to do?

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download