使用 Python 中的列表列表进行矢量化

Vectorize with a list of lists in Python

我的最终目标是为 for 循环使用矢量化的 numpy 解决方案。如果其元素未在原始元素中给出,则此循环会为每个元素创建来自另一个列表的随机样本。但是,for 循环的输入是列表的列表。我不知道如何对列表列表应用 numpy 向量化。一个可重现的例子在这里:

import random

list_of_all_items = [1, 2, 3, 4, 12, 21, 23, 42, 93]
seen_formats = [[1, 2, 3, 4], [2,23, 21, 3], [12, 42, 93, 1]]


not_seen_formats = []
for seen in seen_formats:
    not_seen_formats.append(random.sample([format_ for format_ in list_of_all_items if format_ not in seen],
                            len(seen) * 1))

到目前为止我尝试的是:

import numpy as np

np.where(np.in1d(np.random.choice(list_of_all_items, 2, replace = False), np.asarray(seen_formats)))
>> (array([0, 1], dtype=int64),)

遗憾的是,这毫无意义。我想要返回的是一个数组,它应该包含给定列表列表的随机样本,例如:

>> array([[12, 21], # those numbers should be random numbers
          [ 1,  4],
          [ 2,  3]])
import numpy as np
np.random.seed(42)

list_of_all_items = np.array([1, 2, 3, 4, 12, 21, 23, 42, 93])
seen_formats = np.array([[1, 2, 3, 4], [2,23, 21, 3], [12, 42, 93, 1]])

print(list_of_all_items, '\n')
print(seen_formats, '\n')

def select(a, b):
    return np.random.choice(a=np.setdiff1d(b, a), size=a.size, replace=False)

selection = np.apply_along_axis(func1d=select, axis=1, arr=seen_formats, b=list_of_all_items)
print(selection)

# Alternatively:
# select_vect = np.vectorize(select, excluded=['b'], signature='(m),(n)->(m)')
# selection2  = select_vect(seen_formats, list_of_all_items)
# print(selection2)

输出:

[ 1  2  3  4 12 21 23 42 93] 

[[ 1  2  3  4]
 [ 2 23 21  3]
 [12 42 93  1]] 

[[21 93 23 12]
 [42  4 12  1]
 [ 3  2 21 23]]