2014-05-10 6 views
1

파일의 목록에서 두 개의 새 목록을 나누려고합니다 : 부분 집합 A와 부분 집합 B 즉, 부분 집합 A의 요소 (정수)는 부분 집합 B와 같아야합니다. 이 프로그램은 방법으로 문제를 해결하기 위해 되돌아 사용합니다) 그러나 나는 점점 오전 :.역 추적 typeerror 파이썬

Subset A: <map object at 0x311b110> 
Subset B: <map object at 0x311b190> 

및 오류 :

 line 93, in getSuccessors 
    cfgA.subA.append(config.input[config.index]) 
TypeError: 'map' object is not subscriptable 

전에서지도를 표시 생성자 함수는 다음과 같습니다

def mkPartitionConfig(filename): 
    """ 
    Create a PartitionConfig object. Input is: 
     filename # name of the file containing the input data 

    Returns: A config (PartitionConfig) 
    """ 


    pLst = open(filename).readline().split() 
    print(pLst) 
    config = PartitionConfig 
    config.input = map(int, pLst) 
    config.index = 0 
    config.subA = [] 
    config.subB = [] 
    return config 
,

그리고 난 오류가 무엇입니까 기능 : 내가 여기에 잘못 뭐하는 거지

def getSuccessors(config): 
    """ 
    Get the successors of config. Input is: 
     config: The config (PartitionConfig) 
    Returns: A list of successor configs (list of PartitionConfig) 
    """ 

    cfgA = deepcopy(config) 
    cfgB = deepcopy(config) 
    cfgA.subA.append(config.input[config.index]) 
    cfgB.subB.append(config.input[config.index]) 
    cfgA += 1 
    cfgB += 1 

    return [configA, configB] 

를?

+1

당신이지도 (INT, pLst)를 사용하는 이유는 무엇입니까? –

답변

2

오류 메시지에서 알 수 있듯이 map 개체는 아래 첨자로 묶을 수 없습니다 (대괄호). 지도는 의 유형이며 파이썬에서는입니다. 데이터를 가져 오는 유일한 방법은 한 번에 한 요소 씩 반복하는 것입니다. 아래 첨자로 쓰려면 목록으로 저장해야합니다.

config.input = list(map(int, pLst)) 

이 간단한 예제에서 알 수 있듯이지도 개체를 아래 첨자로 사용할 수 없습니다.

>>> x = [0, 1, 23, 4, 5, 6, 6] 
>>> y = map(str, x) 
>>> y 
<map object at 0x02A69DB0> 
>>> y[1] 
Traceback (most recent call last): 
    File "<pyshell#4>", line 1, in <module> 
    y[1] 
TypeError: 'map' object is not subscriptable 

그리고지도 객체에서 데이터를 얻을 수 있습니다 :

>>> for i in y: 
    print(i) 


0 
1 
23 
4 
5 
6 
6 
+0

감사합니다. – user3408174

0

지도 개체를 목록으로 변환해야합니다.

list(yourmapobject)