是否可以保存和访问包含不同长度元素的数组或列表?例如,我想保存 r,a data=[s,r,a,se]
是标量,但 s
和 se
是一个包含 4 个元素的数组。(在蟒蛇语言中(
例如一次:(s,r,a,se)
在不同的时间是不同的
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]]
我如何定义包含它们的数组以便能够调用它们,类似于以下代码:
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]
另外,我如何提取(查找(data
中有一个特殊[stest,rtest,atest,setest]
(stest,setest
有 4 个元素(
例如:我想看看我是否可以在数据中找到类似于以下内容的[[1,2,3,4],5,6,[7,8,9,10]]
:[ [[1,2,3,4],5,6,[7,8,9,10]] ,[[...,...,...,...],...,..., [...,...,...,...]], [[18,20,31,42],53,666,[27,48,91,120]]]
如果我没有找到,我会附加到它,否则什么都不会发生!
您可以将它们添加到新列表中:
new_list = [s, r, a, se]
但是您必须小心管理此列表
# 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
样本输出:
{'s': [1, 3, 4, 6], 'r': 5, 'a': 7, 'se': [11, 12, 13, 14]}
3
[11, 12, 13, 14]
为了写入此内容,您需要执行以下操作:
my_dict['se'] = [15, 16, 17, 18]
或
my_dict['se'][2] = 19