Python 从生成的多处理对象中提取矩阵
以下代码生成0,1个值的矩阵:Python 从生成的多处理对象中提取矩阵,python,python-2.7,multiprocessing,Python,Python 2.7,Multiprocessing,以下代码生成0,1个值的矩阵: def func(num): X = [random.randint(0, 2 ** 16) for _ in range(num)] X = list(set(X)) X = [('{0:0' + str(16) + 'b}').format(x) for x in X] X = np.asarray([list(map(int, list(x))) for x in X], dtype=np.int8) return X
def func(num):
X = [random.randint(0, 2 ** 16) for _ in range(num)]
X = list(set(X))
X = [('{0:0' + str(16) + 'b}').format(x) for x in X]
X = np.asarray([list(map(int, list(x))) for x in X], dtype=np.int8)
return X
mlp = multiprocessing.Pool(multiprocessing.cpu_count()-1)
X = mlp.map(func, [num])
print X
mlp.close()
mlp.join()
矩阵X
具有以下输出:
[array([[1, 1, 0, ..., 1, 1, 1],
[0, 1, 1, ..., 1, 0, 0],
[0, 0, 0, ..., 1, 0, 0],
...,
[1, 0, 1, ..., 1, 1, 0],
[0, 0, 1, ..., 0, 0, 0],
[1, 0, 1, ..., 0, 0, 0]], dtype=int8)]
但我只想:
[[1, 1, 0, ..., 1, 1, 1],
[0, 1, 1, ..., 1, 0, 0],
[0, 0, 0, ..., 1, 0, 0],
...,
[1, 0, 1, ..., 1, 1, 0],
[0, 0, 1, ..., 0, 0, 0],
[1, 0, 1, ..., 0, 0, 0]]
如何执行此操作?func的定义返回一个numpy数组,而不是本机python列表。如果希望从该函数获取本机Python列表,可以在numpy数组对象上使用该方法。如果func()需要返回numpy数组而不是本机Python列表,原因是什么
import multiprocessing
import numpy as np
import random
def func(num):
X = [random.randint(0, 2 ** 16) for _ in range(num)]
X = list(set(X))
X = [('{0:0' + str(16) + 'b}').format(x) for x in X]
X = np.asarray([list(map(int, list(x))) for x in X], dtype=np.int8)
return X
mlp = multiprocessing.Pool(multiprocessing.cpu_count()-1)
X = mlp.map(func, [10])
mlp.close()
mlp.join()
data = X[0].tolist()
print(data)
你能分享func和num的定义吗?