如何在不连接的情况下为python列表编制索引?

如何在不连接的情况下为python列表编制索引?,python,numpy,indexing,slice,Python,Numpy,Indexing,Slice,假设我有这样的输入: bl = [[0,1,2,3,4,5], [6,7,8,9], [10,11,12,13]] idx = [10, 6, 3, 4, 9, 2] out = [[3,4,2], [6,9], [10]] import numpy as np bl = [[0,1,2,3,4,5], [6,7,8,9], [10,11,12,13]] idx = [10, 6, 3, 4, 9, 2] milestones = np.cumsum(np.array([len(el) fo

假设我有这样的输入:

bl = [[0,1,2,3,4,5], [6,7,8,9], [10,11,12,13]]
idx = [10, 6, 3, 4, 9, 2]
out = [[3,4,2], [6,9], [10]]
import numpy as np
bl = [[0,1,2,3,4,5], [6,7,8,9], [10,11,12,13]]
idx = [10, 6, 3, 4, 9, 2]
milestones = np.cumsum(np.array([len(el) for el in bl])).tolist()
milestones = [0,] + milestones

res = [[] for _ in bl]
for ix in idx:
    for i, m in enumerate(milestones):
        if ix < m:
            res[i-1].append(ix)
            break
我希望输出是这样的:

bl = [[0,1,2,3,4,5], [6,7,8,9], [10,11,12,13]]
idx = [10, 6, 3, 4, 9, 2]
out = [[3,4,2], [6,9], [10]]
import numpy as np
bl = [[0,1,2,3,4,5], [6,7,8,9], [10,11,12,13]]
idx = [10, 6, 3, 4, 9, 2]
milestones = np.cumsum(np.array([len(el) for el in bl])).tolist()
milestones = [0,] + milestones

res = [[] for _ in bl]
for ix in idx:
    for i, m in enumerate(milestones):
        if ix < m:
            res[i-1].append(ix)
            break
背后的逻辑是这样的:

bl = [[0,1,2,3,4,5], [6,7,8,9], [10,11,12,13]]
idx = [10, 6, 3, 4, 9, 2]
out = [[3,4,2], [6,9], [10]]
import numpy as np
bl = [[0,1,2,3,4,5], [6,7,8,9], [10,11,12,13]]
idx = [10, 6, 3, 4, 9, 2]
milestones = np.cumsum(np.array([len(el) for el in bl])).tolist()
milestones = [0,] + milestones

res = [[] for _ in bl]
for ix in idx:
    for i, m in enumerate(milestones):
        if ix < m:
            res[i-1].append(ix)
            break
首先,我可以将
lb_cat
视为一些串联版本:

lb_cat = [0,1,2,3,4,5,6,7,8,9,10,11,12,13]
然后从串联列表中索引:

pick = lb_cat[idx] = [10, 6, 3, 4, 9, 2]
最后,将拾取的元素分配回每个组,以获得

 out = [[3,4,2], [6,9], [10]]
困难在于我不能使用像concatenated这样的操作,因为我的输入不是标准的python列表,它不支持连接操作

我想做的是从一个对象列表中挑选,其中包含“连接”视图中的索引,但实际上我从列表的每个元素中挑选
lb

如何以高效的python方式实现这一点

=============
编辑:

我实现了一个缓慢的版本,如下所示:

bl = [[0,1,2,3,4,5], [6,7,8,9], [10,11,12,13]]
idx = [10, 6, 3, 4, 9, 2]
out = [[3,4,2], [6,9], [10]]
import numpy as np
bl = [[0,1,2,3,4,5], [6,7,8,9], [10,11,12,13]]
idx = [10, 6, 3, 4, 9, 2]
milestones = np.cumsum(np.array([len(el) for el in bl])).tolist()
milestones = [0,] + milestones

res = [[] for _ in bl]
for ix in idx:
    for i, m in enumerate(milestones):
        if ix < m:
            res[i-1].append(ix)
            break
将numpy导入为np
bl=[[0,1,2,3,4,5],[6,7,8,9],[10,11,12,13]]
idx=[10,6,3,4,9,2]
里程碑=np.cumsum(np.array([len(el)表示bl中的el])).tolist()
里程碑=[0,]+里程碑
res=[[]表示【】在bl中】
对于idx中的ix:
对于我,我在列举(里程碑):
如果ix

这对我有用,但太慢了。我有没有办法加快速度?

我不确定我是否正确理解了你的问题,但我希望这将是一个充分的答案

x=0   //to count the total number of numbers with concatination
out = []   //to store final result
for i in bl:
    temp = []
    for j in i:
        if x in idx:     //to check if you want xth indexed element
            temp.append(j)
        x+=1
    if len(temp)>0:      //only append if has an index in idx, i am not sure if you want this
        out.append(temp)
print(out)    //to print final output

如果idx中的x是什么意思?我希望我能使用更有效的方法,所以我需要避免天真的for循环?我还有其他选择吗?如果idx中的x检查元素x是否在列表idx中。这里的运行时O(nm),其中n是bl中的元素总数,m是len(idx),请记住,大多数fast
numpy
代码依赖于“矩形”数组,即n维数组。它以某种快速标准化的方式对整个阵列进行操作。您的
bl
包含不同长度的列表。这很好地表明需要某种Python级别的迭代。您的迭代可能会尽可能好。