Ask Your Question
0

Creating list within an outer for loop counter

asked 2017-07-08 20:43:11 +0200

happys5 gravatar image

Hello,

I want to create one list for L. I gave an example from the list tutorial(list1) that does what I want to do. I believe I have to include the for loop inside the brackets [] but cannot figure it out. I do not know how to program in Python. I am learning C++ now and hope to learn Python next.

for i in range (1,100,1):
    div = divisors(i)
    length = len(div)
    L = [sum(div[k] for k in range(length))-i]
    print i, 

    print L

list1 =[j for j in range(6)] 
print list1

Thanks

edit retag flag offensive close merge delete

Comments

Please describe in words, mathematically what you want.

It may be you want

[ sigma(k) for k in range(1..99) ]

Just google for python list comprehension to see how lists can be defined "almost mathematically".

dan_fulea gravatar imagedan_fulea ( 2017-07-09 01:04:26 +0200 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2017-07-09 00:58:34 +0200

happys5 gravatar image

updated 2017-07-09 01:02:18 +0200

Hello,

I finally figured out how to create the above in one list. Instead of multiple rows with one element, I have one row with multiple elements. I don't know if that is the right "descriptive language" in Sage or python talk so just copy and paste, compare to above and you will see what I mean.

L1 =[divisors(i) for i in range(1,100,1)] 
print L1

L2 =[sum(divisors(i))-i for i in range(1,100,1)]
print L2
edit flag offensive delete link more

Comments

OK, so you wanted [ sigma(k)-k for k in range(1,100) ] , since using the L2 from the above code...

sage: L2 == [ sigma(k)-k for k in range(1,100) ]
True

(The sigma function is already summing the divisors, per default taken at first power. Please realize that from the post it is hard to know which list was desired. Even having the answer, this is not fully clear, for instance - do we need also the list L1? What for? Why do we need L2? Note that structurally the sum of all divisors is the more structural function, since it is algebraically multiplicative, for instance sigma(32) * sigma(49) == sigma( 32*49 ) delivers True...)

dan_fulea gravatar imagedan_fulea ( 2017-07-09 13:00:46 +0200 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2017-07-08 20:43:11 +0200

Seen: 1,290 times

Last updated: Jul 09 '17