Python 基于其他列表将列表拆分为列表的最快方法

Python 基于其他列表将列表拆分为列表的最快方法,python,list,algorithm,performance,partitioning,Python,List,Algorithm,Performance,Partitioning,假设我有一个列表,其中包含0到9范围内的5个唯一整数 import random lst = random.sample(range(10), 5) 我还有一个列表列表,它是通过将0到19的整数拆分为6组得到的: partitions = [[8, 12], [2, 4, 16, 19], [1, 6, 7, 13, 14, 17], [3, 15, 18], [5, 9, 10, 11], [0]] 现在,我想基于引用分区来拆分lst。 例如,如果我有 lst = [0, 1, 6, 8,

假设我有一个列表,其中包含0到9范围内的5个唯一整数

import random
lst = random.sample(range(10), 5)
我还有一个列表列表,它是通过将0到19的整数拆分为6组得到的:

partitions = [[8, 12], [2, 4, 16, 19], [1, 6, 7, 13, 14, 17], [3, 15, 18], [5, 9, 10, 11], [0]]
现在,我想基于引用分区来拆分
lst
。 例如,如果我有

lst = [0, 1, 6, 8, 9]
我希望输出的列表如下所示:

res = [[0], [1, 6], [8], [9]]

我希望算法尽可能快。有什么建议吗?

我不知道这是不是最快的算法,但它确实有效

res=[]

for sublist in partitions: # go through all sublists in partitions
    match = [i for i in lst if i in sublist] # find matching numbers in sublist and lst
    if match: # if it is empty don't append it to res
        res.append(match)
# at this point res is [[8], [1, 6], [9], [0]]                                                                                                         
print(sorted(res)) # use sorted to get desired output
import random
lst = random.sample(range(10), 5)
partitions = [[8, 12], [2, 4, 16, 19], [1, 6, 7, 13, 14, 17], [3, 15, 18], [5, 9, 10, 11], [0]]
sequence = []
result = []

for i in range(5):
    for j in range(len(partitions)):
        if lst[i] in partitions[j]:
            if j in sequence:
                where = sequence.index(j)
                result[where] += [lst[i]]
            else:
                result += [[lst[i]]]
                sequence += [j]
            break
print(result)

哪一个是常数?您是否需要基于相同的
分区为多个
lst
执行此项工作,或者反之亦然,或者每个任务对这两个分区都使用不同的值?当然
分区
是常数。我不明白为什么描述中不清楚。