ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sun, 09 Jul 2017 13:00:46 +0200Creating list within an outer for loop counterhttps://ask.sagemath.org/question/38199/creating-list-within-an-outer-for-loop-counter/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
ThanksSat, 08 Jul 2017 20:43:11 +0200https://ask.sagemath.org/question/38199/creating-list-within-an-outer-for-loop-counter/Comment by dan_fulea for <p>Hello,</p>
<p>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. </p>
<pre><code>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
</code></pre>
<p>Thanks</p>
https://ask.sagemath.org/question/38199/creating-list-within-an-outer-for-loop-counter/?comment=38202#post-id-38202Please 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".Sun, 09 Jul 2017 01:04:26 +0200https://ask.sagemath.org/question/38199/creating-list-within-an-outer-for-loop-counter/?comment=38202#post-id-38202Answer by happys5 for <p>Hello,</p>
<p>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. </p>
<pre><code>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
</code></pre>
<p>Thanks</p>
https://ask.sagemath.org/question/38199/creating-list-within-an-outer-for-loop-counter/?answer=38201#post-id-38201Hello,
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 L2Sun, 09 Jul 2017 00:58:34 +0200https://ask.sagemath.org/question/38199/creating-list-within-an-outer-for-loop-counter/?answer=38201#post-id-38201Comment by dan_fulea for <p>Hello, </p>
<p>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. </p>
<pre><code>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
</code></pre>
https://ask.sagemath.org/question/38199/creating-list-within-an-outer-for-loop-counter/?comment=38204#post-id-38204OK, 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...)Sun, 09 Jul 2017 13:00:46 +0200https://ask.sagemath.org/question/38199/creating-list-within-an-outer-for-loop-counter/?comment=38204#post-id-38204