1

番号のリストがあります:

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

私がする必要があるのは、リスト内の数字の合計を計算し、360 で割って円の部分を計算することです。この例では、32 になります。これを以下で行いました。

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

私が抱えている問題は、ループを実行するたびに最後の番号で番号 (heading_deg) を追加する必要があることです。例えば

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

などなど

何か案は??現時点では、32 回、11 回返されます。

ありがとう!!

4

5 に答える 5

2

累積合計を探していると思います:

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
于 2013-08-28T13:32:58.253 に答える
1

別の回答を投稿して申し訳ありませんが、あなたがやりたいことは、コードに表示されているものとは異なると思います。これがあなたが望むものかどうか見てください:

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

試してみると:

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

最後は 360° を印刷する必要があります。

于 2013-08-28T14:41:13.853 に答える