0

Is it possible to save and access array or list containing elements with different length? For instance, I want to save data=[s,r,a,se] r,a are scalar but s and se are an arrays with 4 elements.(in python language)

For instance in one time:(s,r,a,se) are different in different times

 s=[1,3,4,6] r=5 a=7 se=[11,12,13,14] 
 data=[s,r,a,se]=[[1,3,4,6],5,7,[11,12,13,14]]

How I can define the array containing them to be able to call them similar to the following code:

s=[data[0] for data in minibatch]
r=[data[1] for data in minibatch]
a=[data[2] for data in minibatch]
se=[data[3] for data in minibatch]

Also, how I can extract (Find) that is there a special[stest,rtest,atest,setest] in data (stest,setest are with 4 elements)

For instance: I want to see can I find [[1,2,3,4],5,6,[7,8,9,10]] in data which is something similar to: [ [[1,2,3,4],5,6,[7,8,9,10]] ,[[...,...,...,...],...,..., [...,...,...,...]], [[18,20,31,42],53,666,[27,48,91,120]]]

If I did not find I append to it otherwise nothing is happened!

fila
  • 25
  • 7

2 Answers2

1

You can add them in a new list:

 new_list = [s, r, a, se]

But you'll have to be careful managing this list

0
# This is a great opportunity to explore dictionaries

# lets take the examples you have given in variales
s=[1,3,4,6] 
r=5 
a=7 
se=[11,12,13,14]

# make a dictionary out of them, with keys which are 
# the same as the variable name
my_dict = {'s':s,
           'r':r,
           'a':a,
           'se':se}

# lets see what that looks like
print(my_dict)
print()

# to retrieve 2nd (=ix=1) element of s
# the format to do this is simple
# ditionary_variable_name['the string which is the key'][index_of_the_list]
s2 = my_dict['s'][1]
print(s2)
print()

# to retrieve all of se
se_retrieved = my_dict['se']
print(se_retrieved)

# you can further experiment with this

output of sample:

{'s': [1, 3, 4, 6], 'r': 5, 'a': 7, 'se': [11, 12, 13, 14]}

3

[11, 12, 13, 14]

In order to write to this, you need to do something like this:

my_dict['se'] = [15, 16, 17, 18]

or

my_dict['se'][2] = 19
Edwin van Mierlo
  • 2,398
  • 1
  • 10
  • 19
  • 1
    Thanks but all of them are variable(s,a,r,se) that changing during the several iterations! How we can write them? – fila Apr 23 '18 at 18:12
  • Thanks Edwin, but I think you did not understand my question correctly, my question is that how I can find that for instance: [[1,2,3,4],5,6,[7,8,9,10]] is existed in data which is something similar to: [ [[1,2,3,4],5,6,[7,8,9,10]] ,[[...,...,...,...],...,..., [...,...,...,...]], [[18,20,31,42],53,666,[27,48,91,120]]] – fila Apr 27 '18 at 19:57