1

I have a list of numbers:

list1 = [33, 11, 42, 53, 12, 67, 74, 34, 78, 10, 23]

What I need to do is calculate the total amount of numbers in the list, then divide by 360 to work out portions of a circle. For this example it would be 32. Which I have done below:

def heading():
    for heading_loop in range(len(list1)):
        heading_deg = (360 / len(list1))
    return heading_deg

The problem I have is that I need to have the number (heading_deg) append by the last number everytime it runs throught the loop. E.g.

run 1:  32
run 2:  64
run 3:  96
run 4:  128

etc etc

Any ideas?? At the moment all it does it return 32, 11 times.

Thanks!!

1
  • So the portions of the circle are divided equally? Commented Aug 28, 2013 at 13:32

5 Answers 5

2

I guess you're looking for cumulative sum:

def func(list1):
    tot_sum = 0
    add = 360/len(list1)
    for _ in xrange(len(list1)):
        tot_sum += add
        yield tot_sum

>>> for x in func(list1):
    print x


32
64
96
128
160
192
224
256
288
320
352
Sign up to request clarification or add additional context in comments.

Comments

1

I'm sorry I've posted another answer but I think what you wanna do is different from what you show in your code. Have a look if this is what you want:

def heading():
    result=[] #create a void list
    numbersum=sum(list1) #"calculate the total amount of numbers in the list"
# e.g. if list=[1,1,2] sum(list) return 1+1+2. 
    for i in range(len(list1)):
         result.append(((list1[i])/float(numbersum))*360) # calculate portion of the circle ("then divide by 360 to work out portions of a circle") 
#in this case if you have a list A=[250,250] the function will return two angle of 180° 
    #however you can return portion of the circle in percent e.g. 0.50 is half a circle 1 is the whole circle simply removing "*360" from the code above 
    return result

If you try:

 test=heading()
 print test
 print sum(test)

The last should print 360°.

Comments

0

I don't undersand what you want. It's normal the same value is returned, you don't use heading_loop. So, heading_deg = (360 / len(list1)) is aleready the same result.

How you have 32, then 64, then 96 in the iteration ?

Comments

0

Just use your loop counter & append the calculate result into a list.

list1 = [33, 11, 42, 53, 12, 67, 74, 34, 78, 10, 23]

def heading():
    heading_deg = []
    for heading_loop in range( len( list1 ) ):
        heading_deg.append( ( 360 / len( list1 ) ) * ( heading_loop + 1 ) )
    return heading_deg

Return value is:

[32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352]

Comments

0

I assume the portions of the circle are equally distributed.

The problem with your code is that although heading_deg is computed multiple times, it's always computed the same way, since 360/len(list1) never changes. As Ashiwni points out, you do need a cumulative sum. In case you need to return the cumulative sum as function output, you can use a generator:

def heading():
    deg = (360 / len(list1))
    for heading_loop in range(len(list1)):
        yield (heading_loop+1)*deg

To use the generator:

heading_deg_gen = heading()
print heading_deg_gen.next() # prints 32
print heading_deg_gen.next() # prints 64

# or, you can loop through the values
for heading_deg in heading_deg_gen:
    print heading_deg # prints the rest of the values up to 360

Also, by using integer arithmetic, you are losing some precision here. This can be fine and even desired in some cases, but if you'd rather have a more precise answer, use 360.0/len(list1) instead.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.