Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从生成的多处理对象中提取矩阵_Python_Python 2.7_Multiprocessing - Fatal编程技术网

Python 从生成的多处理对象中提取矩阵

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

以下代码生成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

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的定义吗?