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.Fri, 24 Nov 2017 11:08:02 +0100remember and collect values/outputhttps://ask.sagemath.org/question/39749/remember-and-collect-valuesoutput/Hello, I am working with a function that involves random values. Thus, every time I run my code I get different values(in my case it is a set of values). When I get my output I want Sage to remember it and collect it every time I run my code. At the end I'd like to get a list of sets that has been accumulated every time I ran my code. Is there a way to do it?
I tried to research this topic and haven't found anything except saving a list to .txt file.Thu, 23 Nov 2017 21:07:44 +0100https://ask.sagemath.org/question/39749/remember-and-collect-valuesoutput/Comment by dan_fulea for <p>Hello, I am working with a function that involves random values. Thus, every time I run my code I get different values(in my case it is a set of values). When I get my output I want Sage to remember it and collect it every time I run my code. At the end I'd like to get a list of sets that has been accumulated every time I ran my code. Is there a way to do it?</p>
<p>I tried to research this topic and haven't found anything except saving a list to .txt file.</p>
https://ask.sagemath.org/question/39749/remember-and-collect-valuesoutput/?comment=39751#post-id-39751One can set the seed, maybe, to make the results reconstructible. For instance:
sage: import random
sage: random.seed(20171123)
sage: for k in range( 3 ):
....: print random.uniform(0,1)
....:
0.797007996491
0.714119918018
0.683650988237
sage: random.seed(20171123)
sage: for k in range( 3 ):
....: print random.uniform(0,1)
....:
0.797007996491
0.714119918018
0.683650988237
Now to the question, let us use a list that records the random numbers in between...
sage: random.seed(20171123)
sage: random_values = [] # and we append
sage: for k in range( 3 ):
....: z = random.uniform(0,1)
....: random_values.append( z )
....: # do stuff
giving the list of the three values...Thu, 23 Nov 2017 21:25:42 +0100https://ask.sagemath.org/question/39749/remember-and-collect-valuesoutput/?comment=39751#post-id-39751Comment by dan_fulea for <p>Hello, I am working with a function that involves random values. Thus, every time I run my code I get different values(in my case it is a set of values). When I get my output I want Sage to remember it and collect it every time I run my code. At the end I'd like to get a list of sets that has been accumulated every time I ran my code. Is there a way to do it?</p>
<p>I tried to research this topic and haven't found anything except saving a list to .txt file.</p>
https://ask.sagemath.org/question/39749/remember-and-collect-valuesoutput/?comment=39752#post-id-39752Which is now the question more exactly?! The used data can be of course stored in the one or the other way...Thu, 23 Nov 2017 21:26:35 +0100https://ask.sagemath.org/question/39749/remember-and-collect-valuesoutput/?comment=39752#post-id-39752Comment by tmonteil for <p>Hello, I am working with a function that involves random values. Thus, every time I run my code I get different values(in my case it is a set of values). When I get my output I want Sage to remember it and collect it every time I run my code. At the end I'd like to get a list of sets that has been accumulated every time I ran my code. Is there a way to do it?</p>
<p>I tried to research this topic and haven't found anything except saving a list to .txt file.</p>
https://ask.sagemath.org/question/39749/remember-and-collect-valuesoutput/?comment=39768#post-id-39768@dan_fulea, i am not sure about the user's intentions, but it might be that the computation is long so that when she finds something interesting, she wants to store it, to not only to be able to recompute it. Keeping the seed when doing randomized experiments is a good idea in any case.Fri, 24 Nov 2017 11:08:02 +0100https://ask.sagemath.org/question/39749/remember-and-collect-valuesoutput/?comment=39768#post-id-39768Answer by tmonteil for <p>Hello, I am working with a function that involves random values. Thus, every time I run my code I get different values(in my case it is a set of values). When I get my output I want Sage to remember it and collect it every time I run my code. At the end I'd like to get a list of sets that has been accumulated every time I ran my code. Is there a way to do it?</p>
<p>I tried to research this topic and haven't found anything except saving a list to .txt file.</p>
https://ask.sagemath.org/question/39749/remember-and-collect-valuesoutput/?answer=39766#post-id-39766If you want to collect your sets into a list, you can start with the empty list, and append your sets to that list:
sage: L = []
sage: L.append({1,2,3})
sage: L.append({12,13})
sage: L
[{1, 2, 3}, {12, 13}]
At the end of the day, you can save that list on your disk:
sage: save(L,'/path/to/my_list.sobj')
The other day, you can load that list again:
sage: L = load('/path/to/my_list.sobj')
sage: L
[{1, 2, 3}, {12, 13}]
Then continue to append sets into it:
sage: L.append({123})
sage: L
[{1, 2, 3}, {12, 13}, {123}]
Then sage it again (on the same or a different file if you want to keep the history):
sage: save(L,'/path/to/my_list.sobj')
And so on.
Fri, 24 Nov 2017 10:59:06 +0100https://ask.sagemath.org/question/39749/remember-and-collect-valuesoutput/?answer=39766#post-id-39766