是否可以在 python 中保存、访问和搜索包含不同长度元素的数组或列表?

Is it possible to save and access and search an array or list containing elements with different length in python?

是否可以保存和访问包含不同长度元素的数组或列表?例如,我想保存 data=[s,r,a,se] r,a 是标量,但 sse 是一个包含 4 个元素的数组。(在 python 语言中)

比如一次:(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]

此外,我如何提取 (Find) 在 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