我有一个名为my_list的python numpy列表,如下所示,我可以访问元素,如my_list[0](第一级)和my_list[0][0](第二级)等等。
[array(['27103', '27106', '27104', '87115'],
dtype='
array(['20049', '20033', '655155'],
dtype='
对于列表第二级中的每个元素,我创建另一个列表,每个列表包含3个元素。所以对于元素“27103”,有一个长度为3的新列表,对于“27106”,还有一个包含3个元素的列表。
我的问题是如何组合新列表,以便计算到第一级。我写了下面的代码:
temp_list = []
temp_list2 = []
for i in range(len(my_list)):
for j in range(len(set(my_list[i]))):
temp_list.append(list(x[np.where(df.index.isin(my_list[i]))[0][j]]))
temp_list2.append(np.concatenate(temp_list))
结果如下:
[array(['111005', '112140', '627372', '639240', '672846', '71138', '10618',
'110619', '639821', '668821', '672079', '672080'], dtype='
array(['111005', '112140', '627372', '639240', '672846', '71138', '10618',
'110619', '639821', '668821', '672079', '672080', '109002',
'112839', '26800', '112137', '652520', '667409', '104506',
'110004', '110955'], dtype='
问题是它从一开始就附加到临时列表2。我如何才能避免这种情况,并且只采取相关的部分,使它看起来像下面这样?
[array(['111005', '112140', '627372', '639240', '672846', '71138', '10618',
'110619', '639821', '668821', '672079', '672080'], dtype='
array(['109002', '112839', '26800', '112137', '652520', '667409', '104506',
'110004', '110955'], dtype='
事先谢谢。