2015-01-28 2 views
3

를 첨자에되지 않는다 'itertools.combinations'개체 :형식 오류 : 나는 실행하려고하면

temp = (twoset2[x][i][0]-twoset[x][i][1]) 

를 내가 얻을 :

TypeError: 'itertools.combinations' object is not subscriptable

내 코드 : 기본적으로

for x in range(0,64): 
    for i in range(0,1): 
     temp = (twoset2[x][i][0]-twoset[x][i][1]) 
     DSET[counter2]= temp 
     temp = 0 
     counter2 += 1 

내가 내가하려고하는 일은 : 좌표의 2 요소 하위 집합 (예 : ((2,0) (3,3))의 목록 (twoset2)이 있습니다. 각 개별 좌표에 액세스하고 xy 사이의 차이를 가져와 DSET에 넣고 실행하려고 할 때 위의 오류가 발생합니다.

도와주세요!

+1

어디'itertools.combinations이다()'코드에서? – GLHF

답변

3

itertools.combinations이 발전기를 돌려 아닌를 명부. 이것이 의미하는 바는 당신이 그것을 반복 할 수 있지만 시도하고있는 것처럼 인덱스를 가진 요소에 의해 요소에 액세스하지 못한다는 것입니다.

대신 당신과 같이 각 조합을 얻을 수 있습니다 :

import itertools 
for combination in itertools.combinations([1,2,3], 2): 
    print combination 

이 제공 :

(1, 2) 
(1, 3) 
(2, 3) 
+0

'a, b = combination'은 필요 없습니다. – GLHF

1

twoset2은 목록이 아닙니다. (인덱싱을 지원하지 않습니다)는 itertools.combinations 객체입니다

>>> import itertools 
>>> itertools.combinations([1, 2, 3], 2) 
<itertools.combinations object at 0x01ACDC30> 
>>> 
>>> twoset2 = itertools.combinations([1, 2, 3], 2) 
>>> twoset2[0] 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
TypeError: 'itertools.combinations' object is not subscriptable 
>>> 

당신이 목록을 원하는 경우 명시 적으로 목록에이 변환해야합니다

twoset2 = list(itertools.combinations(...)) 
관련 문제