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.Sat, 16 Oct 2010 16:05:58 +0200Help! I have a problem with lists!https://ask.sagemath.org/question/7727/help-i-have-a-problem-with-lists/Hello!
I am a real beginner in Sage, I have just started th use it. In fact this is my first programing language, so I have some difficulties with it... Now I needed a command, which determins all the k-element subset of [1,2,...,N]. Since I don't found a command like this, I decided to write a program... :-) And now I simple don't understand while my program doesn't work corrrectly! Could you explain me? It is a bit longish, so sorry for this.
def eve(A,N):
"""
the largest elem in the list A which can
be increased by 1, such that the resulting list contains only
different elements less than N. Originally
A should contain different elements in increasing order.
"""
ev = len(A)-1
while A[ev] == ev+N-len(A):
ev=ev-1
return ev
def rakovetkezo(A,N):
"""
Increases the eve(A,N)nt element by 1, and all the other elements
after this element are succesive natural numbers
"""
Q=A
ev=eve(Q,N)
x=Q[ev]
for i in range(ev,len(Q)):
Q[i]=x+i-ev+1
return Q
def reszhalmazok(N,k):
"""
It should (but it doesn't) return the k-element
subset of [1,2,...,N]
"""
X=range(k)
B=[range(k)]
ev=eve(X,N)
while ev != -1:
X=rakovetkezo(X,N)
B.append(X)
ev = eve(X,N)
return B
Now `reszhalmazok(5,3`) should return `[[0,1,2], [0,1,3],...]` but it returns something completely different, aaand I don't see why, and how could be improve it?
KatikaSat, 16 Oct 2010 05:42:24 +0200https://ask.sagemath.org/question/7727/help-i-have-a-problem-with-lists/Answer by mvngu for <p>Hello! </p>
<p>I am a real beginner in Sage, I have just started th use it. In fact this is my first programing language, so I have some difficulties with it... Now I needed a command, which determins all the k-element subset of [1,2,...,N]. Since I don't found a command like this, I decided to write a program... :-) And now I simple don't understand while my program doesn't work corrrectly! Could you explain me? It is a bit longish, so sorry for this.</p>
<pre><code>def eve(A,N):
"""
the largest elem in the list A which can
be increased by 1, such that the resulting list contains only
different elements less than N. Originally
A should contain different elements in increasing order.
"""
ev = len(A)-1
while A[ev] == ev+N-len(A):
ev=ev-1
return ev
def rakovetkezo(A,N):
"""
Increases the eve(A,N)nt element by 1, and all the other elements
after this element are succesive natural numbers
"""
Q=A
ev=eve(Q,N)
x=Q[ev]
for i in range(ev,len(Q)):
Q[i]=x+i-ev+1
return Q
def reszhalmazok(N,k):
"""
It should (but it doesn't) return the k-element
subset of [1,2,...,N]
"""
X=range(k)
B=[range(k)]
ev=eve(X,N)
while ev != -1:
X=rakovetkezo(X,N)
B.append(X)
ev = eve(X,N)
return B
</code></pre>
<p>Now <code>reszhalmazok(5,3</code>) should return <code>[[0,1,2], [0,1,3],...]</code> but it returns something completely different, aaand I don't see why, and how could be improve it?</p>
<p>Katika</p>
https://ask.sagemath.org/question/7727/help-i-have-a-problem-with-lists/?answer=11731#post-id-11731Use the built-in Python command [itertools.combinations][1]:
sage: import itertools
sage: L = [1..5]; L
[1, 2, 3, 4, 5]
sage: for s in itertools.combinations(L, 3):
....: print s
....:
(1, 2, 3)
(1, 2, 4)
(1, 2, 5)
(1, 3, 4)
(1, 3, 5)
(1, 4, 5)
(2, 3, 4)
(2, 3, 5)
(2, 4, 5)
(3, 4, 5)
[1]: http://docs.python.org/library/itertools.html#itertools.combinationsSat, 16 Oct 2010 07:08:39 +0200https://ask.sagemath.org/question/7727/help-i-have-a-problem-with-lists/?answer=11731#post-id-11731Comment by Katika for <p>Use the built-in Python command <a href="http://docs.python.org/library/itertools.html#itertools.combinations">itertools.combinations</a>:</p>
<pre><code>sage: import itertools
sage: L = [1..5]; L
[1, 2, 3, 4, 5]
sage: for s in itertools.combinations(L, 3):
....: print s
....:
(1, 2, 3)
(1, 2, 4)
(1, 2, 5)
(1, 3, 4)
(1, 3, 5)
(1, 4, 5)
(2, 3, 4)
(2, 3, 5)
(2, 4, 5)
(3, 4, 5)
</code></pre>
https://ask.sagemath.org/question/7727/help-i-have-a-problem-with-lists/?comment=11732#post-id-11732Thank you very much! Indeed it works, and I also found (reading your link it was easy), the command combinations... :-) It will help me a lot writing programs concerning subsets :-)
(Now I am more sure that SAGE works! Just still I don't see why that program written by me doesn't work as nice as the other programs of SAGE :-) ) Sat, 16 Oct 2010 07:58:51 +0200https://ask.sagemath.org/question/7727/help-i-have-a-problem-with-lists/?comment=11732#post-id-11732Answer by Mike Hansen for <p>Hello! </p>
<p>I am a real beginner in Sage, I have just started th use it. In fact this is my first programing language, so I have some difficulties with it... Now I needed a command, which determins all the k-element subset of [1,2,...,N]. Since I don't found a command like this, I decided to write a program... :-) And now I simple don't understand while my program doesn't work corrrectly! Could you explain me? It is a bit longish, so sorry for this.</p>
<pre><code>def eve(A,N):
"""
the largest elem in the list A which can
be increased by 1, such that the resulting list contains only
different elements less than N. Originally
A should contain different elements in increasing order.
"""
ev = len(A)-1
while A[ev] == ev+N-len(A):
ev=ev-1
return ev
def rakovetkezo(A,N):
"""
Increases the eve(A,N)nt element by 1, and all the other elements
after this element are succesive natural numbers
"""
Q=A
ev=eve(Q,N)
x=Q[ev]
for i in range(ev,len(Q)):
Q[i]=x+i-ev+1
return Q
def reszhalmazok(N,k):
"""
It should (but it doesn't) return the k-element
subset of [1,2,...,N]
"""
X=range(k)
B=[range(k)]
ev=eve(X,N)
while ev != -1:
X=rakovetkezo(X,N)
B.append(X)
ev = eve(X,N)
return B
</code></pre>
<p>Now <code>reszhalmazok(5,3</code>) should return <code>[[0,1,2], [0,1,3],...]</code> but it returns something completely different, aaand I don't see why, and how could be improve it?</p>
<p>Katika</p>
https://ask.sagemath.org/question/7727/help-i-have-a-problem-with-lists/?answer=11733#post-id-11733The reason why your program doesn't work is due to the line
Q = A
in `rakovetkezo`. When you do `Q = A`, you make `Q` point to the same object that `A` is pointing to. Then, when you change `Q`, you also change `A`. See the following:
sage: A = [1,2,3]
sage: Q = A
sage: id(Q)
88220880
sage: id(A)
88220880
sage: Q[0] = 100
sage: A
[100, 2, 3]
In order to make a copy of A, you can do something like
Q = A[:]
or
Q = list(A)
or
from copy import copy
Q = copy(A)
Then your program will work just fine.
Sage also has a builtin class for working with with combinations which is a bit more fully-featured than `itertools.combinations`. Here are some examples of this.
sage: S = Combinations(range(5), 3); S
Combinations of [0, 1, 2, 3, 4] of length 3
sage: S.cardinality()
10
sage: S.random_element()
[0, 3, 4]
sage: S.list()
[[0, 1, 2], [0, 1, 3], [0, 1, 4], [0, 2, 3], [0, 2, 4], [0, 3, 4], [1, 2, 3], [1, 2, 4], [1, 3, 4], [2, 3, 4]]
sage: [x for x in S]
[[0, 1, 2], [0, 1, 3], [0, 1, 4], [0, 2, 3], [0, 2, 4], [0, 3, 4], [1, 2, 3], [1, 2, 4], [1, 3, 4], [2, 3, 4]]
Sat, 16 Oct 2010 14:26:54 +0200https://ask.sagemath.org/question/7727/help-i-have-a-problem-with-lists/?answer=11733#post-id-11733Comment by Katika for <p>The reason why your program doesn't work is due to the line</p>
<pre><code>Q = A
</code></pre>
<p>in <code>rakovetkezo</code>. When you do <code>Q = A</code>, you make <code>Q</code> point to the same object that <code>A</code> is pointing to. Then, when you change <code>Q</code>, you also change <code>A</code>. See the following:</p>
<pre><code>sage: A = [1,2,3]
sage: Q = A
sage: id(Q)
88220880
sage: id(A)
88220880
sage: Q[0] = 100
sage: A
[100, 2, 3]
</code></pre>
<p>In order to make a copy of A, you can do something like </p>
<pre><code>Q = A[:]
</code></pre>
<p>or </p>
<pre><code>Q = list(A)
</code></pre>
<p>or</p>
<pre><code> from copy import copy
Q = copy(A)
</code></pre>
<p>Then your program will work just fine.</p>
<p>Sage also has a builtin class for working with with combinations which is a bit more fully-featured than <code>itertools.combinations</code>. Here are some examples of this.</p>
<pre><code>sage: S = Combinations(range(5), 3); S
Combinations of [0, 1, 2, 3, 4] of length 3
sage: S.cardinality()
10
sage: S.random_element()
[0, 3, 4]
sage: S.list()
[[0, 1, 2], [0, 1, 3], [0, 1, 4], [0, 2, 3], [0, 2, 4], [0, 3, 4], [1, 2, 3], [1, 2, 4], [1, 3, 4], [2, 3, 4]]
sage: [x for x in S]
[[0, 1, 2], [0, 1, 3], [0, 1, 4], [0, 2, 3], [0, 2, 4], [0, 3, 4], [1, 2, 3], [1, 2, 4], [1, 3, 4], [2, 3, 4]]
</code></pre>
https://ask.sagemath.org/question/7727/help-i-have-a-problem-with-lists/?comment=22574#post-id-22574Thank you! This was very surprising for me, however I have heard about pointers... I would never find this out! Now I will be more careful copying objects. :-) Sat, 16 Oct 2010 16:05:58 +0200https://ask.sagemath.org/question/7727/help-i-have-a-problem-with-lists/?comment=22574#post-id-22574