2014-04-13 3 views
0

파이썬 multiprocessing 라이브러리를 사용하여 파일을 읽으려고하지만 원하는 결과를 얻지 못했습니다.Python 다중 처리 라이브러리를 사용하는 이상한 동작

TypeError: int() argument must be a string or a number, not 'list' 
0 
20050 

나는 다음과 같은 문제가 있습니다 :

  1. 나는 이유를 이해하지 않습니다 여기에

    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 
    

    내가받을 출력을 간다 : 여기 내가 사용하고 코드를 간다 위의 코드에서 길이가 output은 0이고 변수의 길이는 m입니다. 내게 따르면 outputm, shou 길이는 20050입니다.

  2. 이 경우 TypeError()이 필요한 이유는 무엇입니까? 인수가 partitioner 함수의 목록이 아닌 이유는 무엇입니까?

  3. 디버그 창에서 변수 m의 내용을 보려고하면 내 시스템이 거의 엉망입니다. (우분투 13.10을 사용하고 Pycharm 3.1을 실행 중입니다!) 내가 보려고하는 목록의 내용이 엄청나게 거대하면이 경우를 이해할 수 있습니다.이 경우에는 그렇지 않습니다. 이 목록은 20050 개의 목록으로 구성되며 각 목록에는 6 개의 요소가 있습니다.

이와 관련하여 도움을 주시면 감사하겠습니다.

답변

0

문제점은 매퍼 기능에서 내용을 올바르게 반환하지 못하는 것이 었습니다. 코드가 약간 변경되면 필요에 따라 작동합니다.

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+']) 
0

은 그냥 오류, partitioner 통화를 해결하기 :

int(element[1]) 

을하지만, map_func1element1가 정의 age입니다 :이 두 항목 목록 슬라이스입니다

age = f[17:19] #18-19 

하고있다 자체 목록이므로 int의 유효한 인수가 아닙니다.

다른 사람들에게 샘플을 출력하여 거기에 무엇이 있는지 볼 것을 권합니다.

+0

답장을 보내 주셔서 감사합니다. 나는 그 문제를 알아 냈다. 내 map_func1은 "return [gen, age, htin, wtlbs, ovwt, chwt]"을 반환해야합니다. – Patthebug

관련 문제