First time here? Check out the FAQ!

Ask Your Question
0

Creating list within an outer for loop counter

asked 7 years ago

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

Preview: (hide)

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 ( 7 years ago )

1 Answer

Sort by » oldest newest most voted
0

answered 7 years ago

happys5 gravatar image

updated 7 years ago

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
Preview: (hide)
link

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 ( 7 years ago )

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: 7 years ago

Seen: 1,396 times

Last updated: Jul 09 '17