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.Tue, 09 Jul 2013 20:21:56 +0200create numpy arrays or lists with customiza nameshttps://ask.sagemath.org/question/10337/create-numpy-arrays-or-lists-with-customiza-names/Hi experts!
Im running a script with a for-cycle. In every loop a list (or np array) is generated. I wanna these lists receive the name "list_number_###", where ### is the index of the for-cycle (e.g.: if the for-cycle is " for srange(a,b+1,1)..." i wanna that the np array or list receive the names "list_number_a", "list_number_a+1",..., "list_number_b".
How can i do that?
Waiting for your answers.
Thanks a lot!Tue, 09 Jul 2013 12:28:47 +0200https://ask.sagemath.org/question/10337/create-numpy-arrays-or-lists-with-customiza-names/Answer by Luca for <p>Hi experts!</p>
<p>Im running a script with a for-cycle. In every loop a list (or np array) is generated. I wanna these lists receive the name "list_number_###", where ### is the index of the for-cycle (e.g.: if the for-cycle is " for srange(a,b+1,1)..." i wanna that the np array or list receive the names "list_number_a", "list_number_a+1",..., "list_number_b".</p>
<p>How can i do that?</p>
<p>Waiting for your answers.</p>
<p>Thanks a lot!</p>
https://ask.sagemath.org/question/10337/create-numpy-arrays-or-lists-with-customiza-names/?answer=15212#post-id-15212Have you considered using lists of lists?
list_of_np = []
for i in range(10):
list[i] = my_np_array()
or even, using list comprehensions,
list_of_np = [my_np_array() for i in range(10)]
If you are really determined to have dynamically generated variable names, Python allows you to modify the current module scope via the `globals()` call. You can do
for i in range(10):
globals()["list_number_" + str(i)] = my_np_array()
However in my opinion you'd better stick with one of the two previous forms.Tue, 09 Jul 2013 14:08:20 +0200https://ask.sagemath.org/question/10337/create-numpy-arrays-or-lists-with-customiza-names/?answer=15212#post-id-15212Answer by tmonteil for <p>Hi experts!</p>
<p>Im running a script with a for-cycle. In every loop a list (or np array) is generated. I wanna these lists receive the name "list_number_###", where ### is the index of the for-cycle (e.g.: if the for-cycle is " for srange(a,b+1,1)..." i wanna that the np array or list receive the names "list_number_a", "list_number_a+1",..., "list_number_b".</p>
<p>How can i do that?</p>
<p>Waiting for your answers.</p>
<p>Thanks a lot!</p>
https://ask.sagemath.org/question/10337/create-numpy-arrays-or-lists-with-customiza-names/?answer=15215#post-id-15215As explained by @Luca, it is not a good idea to have indices as a substring of the name of your lists, but as indices: `list_number[i]` makes more sense than `list_number_i`.
Lists indices go from `0` to `length-1`, so if `a` is big (or if the set of indices is sparse), you can use a dictionary instead. For example, assume that the i^th element (more precisely the element whose key is `i`) of your dictionary is the list `[i,i^2]`, you can write:
sage: a = 10
sage: b = 20
sage: dict_number = {}
sage: for i in srange(a, b+1, 1):
....: dict_number[i] = [i, i^2]
Then you can call the element of your dictionary whose key is 13:
sage: dict_number[13]
[13, 169]
Note that the keys of your dictionary can be more than just numbers, for example they can be strings:
sage: dict_number['plop'] = [12,13]
sage: dict_number['plop']
[12, 13]
You can read more about python dictionaries [here](http://docs.python.org/2/tutorial/datastructures.html#dictionaries).
Tue, 09 Jul 2013 20:21:56 +0200https://ask.sagemath.org/question/10337/create-numpy-arrays-or-lists-with-customiza-names/?answer=15215#post-id-15215