Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.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_List_File Io_Multiprocessing_Pycharm - Fatal编程技术网

使用Python多处理库的奇怪行为

使用Python多处理库的奇怪行为,python,list,file-io,multiprocessing,pycharm,Python,List,File Io,Multiprocessing,Pycharm,我试图使用python多处理库读取一个文件,但没有得到想要的结果。下面是我正在使用的代码: import multiprocessing as mp import itertools partitioned = {} partitioned['0-20'] = [] partitioned['20-40'] = [] partitioned['40-60'] = [] partitioned['60+'] = [] output = [] def map_func1(f): # fo

我试图使用python多处理库读取一个文件,但没有得到想要的结果。下面是我正在使用的代码:

import multiprocessing as mp
import itertools

partitioned = {}
partitioned['0-20'] = []
partitioned['20-40'] = []
partitioned['40-60'] = []
partitioned['60+'] = []
output = []

def map_func1(f):
    # for line in f:
    gen = f[14:15] #15 1=male 2=female
    age = f[17:19] #18-19
    htin = f[1947:1950] #1948-1950 tall in inches, self reported !888! !999!
    wtlbs = f[1950:1953] #1951-1953 wt in lbs, self reported !888! !999!
    ovwt = f[1963:1964] #1964 consider myself overweight 1,under 2,over 3, !8!, !9!
    chwt = f[1964:1965] #1965 change weight or stay same 1=more, 2=less, 3=same, !8!, !9!
    output.append([gen, age, htin, wtlbs, ovwt, chwt])
    return output

def partitioner(m):
    for element in m:
        if int(element[1]) < 20:
            output['0-20'].append(element)
        elif int(element[1]) < 40:
            output['20-40'].append(element)
        elif int(element[1]) < 60:
            output['40-60'].append(element)
        else:
            output['60+'].append(element)

    return partitioned

if __name__ == "__main__":
    pool = mp.Pool(processes=3)
    f = open('adult.dat')
    m = pool.map(map_func1, f)
    print len(output)
    print len(m)
    p = partitioner(m)
    print p
我有以下问题:

我不明白为什么在前面提到的代码中,输出的长度是0,变量m的长度是20050。根据我的说法,output和m的长度都应该是20050

为什么在这种情况下会出现打字错误?为什么参数不能是partitioner函数中的列表

当我试图在调试窗口中查看变量m的内容时,我的系统几乎崩溃了。我正在使用Ubuntu13.10并在上面运行Pycharm 3.1!如果我试图查看的列表内容非常庞大,我可以理解这一点,但在本例中,它们不是。它是一个由20050个列表组成的列表,每个列表有6个元素


我们将非常感谢您在这方面提供的任何帮助。

为了解决您的错误,partitioner致电:

但是,根据map_func1,element1是年龄,定义如下:

age = f[17:19] #18-19
这是一个两项列表切片,本身就是一个列表,因此不是int的有效参数

对于其他人,我建议你输出一个样本,看看里面有什么,例如

print m[:5]

问题是我没有正确地从映射器函数返回内容。代码中的细微更改可使其按要求工作:

import multiprocessing as mp
import itertools

partitioned = {}
partitioned['0-20'] = []
partitioned['20-40'] = []
partitioned['40-60'] = []
partitioned['60+'] = []

def map_func1(f):
    # for line in f:
    gen = f[14:15] #15 1=male 2=female
    age = f[17:19] #18-19
    htin = f[1947:1950] #1948-1950 tall in inches, self reported !888! !999!
    wtlbs = f[1950:1953] #1951-1953 wt in lbs, self reported !888! !999!
    ovwt = f[1963:1964] #1964 consider myself overweight 1,under 2,over 3, !8!, !9!
    chwt = f[1964:1965] #1965 change weight or stay same 1=more, 2=less, 3=same, !8!, !9!
    return [gen, age, htin, wtlbs, ovwt, chwt]

def partitioner(m):
    for element in m:
        if int(element[1]) < 20:
            partitioned['0-20'].append(element)
        elif int(element[1]) < 40:
            partitioned['20-40'].append(element)
        elif int(element[1]) < 60:
            partitioned['40-60'].append(element)
        else:
            partitioned['60+'].append(element)

    return partitioned

if __name__ == "__main__":
    pool = mp.Pool(processes=3)
    f = open('adult.dat')
    m = pool.map(map_func1, f)
    print m[0]
    p = partitioner(m)
    print len(p['60+'])

谢谢你的回复。我解决了这个问题。我的map_func1必须返回[gen、age、htin、wtlbs、ovwt、chwt]
print m[:5]
import multiprocessing as mp
import itertools

partitioned = {}
partitioned['0-20'] = []
partitioned['20-40'] = []
partitioned['40-60'] = []
partitioned['60+'] = []

def map_func1(f):
    # for line in f:
    gen = f[14:15] #15 1=male 2=female
    age = f[17:19] #18-19
    htin = f[1947:1950] #1948-1950 tall in inches, self reported !888! !999!
    wtlbs = f[1950:1953] #1951-1953 wt in lbs, self reported !888! !999!
    ovwt = f[1963:1964] #1964 consider myself overweight 1,under 2,over 3, !8!, !9!
    chwt = f[1964:1965] #1965 change weight or stay same 1=more, 2=less, 3=same, !8!, !9!
    return [gen, age, htin, wtlbs, ovwt, chwt]

def partitioner(m):
    for element in m:
        if int(element[1]) < 20:
            partitioned['0-20'].append(element)
        elif int(element[1]) < 40:
            partitioned['20-40'].append(element)
        elif int(element[1]) < 60:
            partitioned['40-60'].append(element)
        else:
            partitioned['60+'].append(element)

    return partitioned

if __name__ == "__main__":
    pool = mp.Pool(processes=3)
    f = open('adult.dat')
    m = pool.map(map_func1, f)
    print m[0]
    p = partitioner(m)
    print len(p['60+'])