gpt4 book ai didi

python - 以同样的方式在python中随机分割两个列表

转载 作者:行者123 更新时间:2023-12-01 01:22:41 26 4
gpt4 key购买 nike

我有两个包含相同大小的 numpy 数组的列表。列表list_A有 1000 个项目,其中每个 numpy 数组的大小为 20x20x3list_B大小为 20x8 的 numpy 数组。我想以相同的方式将两个列表随机拆分为 100 个子列表(最后 list_A 的每个子列表包含 100 个 numpy 数组, list_B 的每个子列表也包含 100 个 numpy 数组)。我只为一个列表编写了如何执行此操作的代码:

def partition (list_in, n):
random.shuffle(list_in)
return [list_in[i::n] for i in range(n)]

total_lists_A = partition (list_A, 10)

但是,我想对 list_A 执行同样的操作和list_B同样的方式返回total_lists_Atotal_lists_B

最佳答案

您可以在函数中包装numpy.random.seed,以使其可重现。类似于(根据您的方法):

# note: will not work properly if your two lists are different shapes:
def my_partition(list_in, n):
np.random.seed(1)
idx = np.random.shuffle(list_in)
return [list_in[i::n] for i in range(n)]

或者(稍微不同的方法,应该可行)

def my_partition(list_in, n):
np.random.seed(1)
idx = np.random.choice(range(len(list_in)), len(list_in))
split = np.split(idx, n)
return [list_in[i] for i in split]

关于python - 以同样的方式在python中随机分割两个列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53655222/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com