# remember and collect values/output

Anonymous

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.

edit retag close merge delete

One 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...

( 2017-11-23 21:25:42 +0200 )edit

Which is now the question more exactly?! The used data can be of course stored in the one or the other way...

( 2017-11-23 21:26:35 +0200 )edit

@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.

( 2017-11-24 11:08:02 +0200 )edit

Sort by ยป oldest newest most voted

If 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.

more