2012-08-02 6 views
2

이 코드를 사용하고 있습니다 :동일한 함수는 파이썬에서 반대 순서로 다른 결과를 제공합니다. 왜?

def copy_part_of_space(row,column,lenght): 
    #Copy String to Presentation Space (15) 
    #Prerequisite Connect Presentation Space 
    #Prerequisite function: connect_pcomm(presentation_space)  
    function_number = c_int(8) 
    data_string = create_string_buffer(lenght*2*2) #number of unicode char *2*2 
    lenght = c_int(lenght) 
    ps_position = c_int(((row - 1) * 80)+ column) 
    foo = hllapi(byref(function_number), data_string, byref(lenght), byref(ps_position)) 
    data_string.value 
    return {{ 
     0 : 'The host presentation space contents were copied to the application program. The target presentation space was active, and the keyboard was unlocked.', 
     1 : 'Your program is not connected to a host session.', 
     4 : 'The host presentation space contents were copied. The connected host  presentation space was waiting for host response.', 
     5 : 'The host presentation space was copied. The keyboard was locked.', 
     9 : 'A system error was encountered.', 
     'x' : 'Undocumented error found. Run in circles.', 
     }.get(foo, 'x'),data_string.value} 

아이디어 터미널에서 일부 정보를 복사하는 것입니다; 함수는 상태 정보를 반환해야합니다 (사전 및 0,1,4,5,9, x 매개 변수 사용). 그리고 복사 된 정보 - data_string.value 사용

몇 가지 테스트를 실행하려면 다음 코드를 사용합니다. 위의 기능 : - 첫 번째 줄처럼 당신이 볼 수 있듯이

set(['The host presentation space contents were copied to the application program. The target presentation space was active, and the keyboard was unlocked.', '36343581']) 
    set(['36343663', 'The host presentation space contents were copied to the application program. The target presentation space was active, and the keyboard was unlocked.']) 
    set(['The host presentation space contents were copied to the application program. The target presentation space was active, and the keyboard was unlocked.', '36343708']) 
    set(['36344673', 'The host presentation space contents were copied to the application program. The target presentation space was active, and the keyboard was unlocked.']) 
    set(['36344740', 'The host presentation space contents were copied to the application program. The target presentation space was active, and the keyboard was unlocked.']) 
    set(['The host presentation space contents were copied to the application program. The target presentation space was active, and the keyboard was unlocked.', '36344758']) 
    set(['36344869', 'The host presentation space contents were copied to the application program. The target presentation space was active, and the keyboard was unlocked.']) 

가, 때로는 호스트 응용 프로그램에서 복사 한 것을 이전 상태 정보를 얻을 :

for a in range(15,22): 
    print copy_part_of_space(a,7,8) 

이 결과입니다.

하지만 때로는 상태 정보 앞에 복사 된 정보 (예 : 2 행)가 표시됩니다.

나는 정보를 반환하는 dict를 사용하여와 하지 잘 알고, 그래서 내가 특별히 내가 두 변수를 반환하기 위해 노력하고있어 사실과 혼합 할 때, 그 문제가 될 수도 있겠죠.

왜 이런 일이 일어날 지 설명 할 수 있습니까?

나는 단순히 dict을 사용하고 반환 값을 변수에 저장하기 만하면되지만, 실제로 이것이 더 우아한 해결책이라고 생각합니다. 틀렸습니까?

답변

6

set은 순서가 없습니다 (또는 순서가 임의 임). 대신 주문 된 데이터 유형을 사용하는 것을 제외하고는 아무 것도 할 수 없습니다. set 생성자 {...} 제거하여 예를 들어

:

return { 
    0 : 'The host presentation space contents were copied to the application program. The target presentation space was active, and the keyboard was unlocked.', 
    1 : 'Your program is not connected to a host session.', 
    4 : 'The host presentation space contents were copied. The connected host  presentation space was waiting for host response.', 
    5 : 'The host presentation space was copied. The keyboard was locked.', 
    9 : 'A system error was encountered.', 
    'x' : 'Undocumented error found. Run in circles.', 
    }.get(foo, 'x'), data_string.value 

지금 코드 대신 tuple 복귀 (첫 번째 요소는 "오류 메시지 사전」에 포함되는 어떤 두 번째로부터 조회 결과 data_string.value).

+0

'그들의 순서는 arbitrary'하지만, 당신이 일을 할 때 바뀔 수도있다. – katrielalex

+0

이 경우에 할 일은 튜플을 반환하는 것이다. –

+0

@MarkRansom :'set' 생성자가 제거 될 때 어떤 일이 발생합니다. –

3

구체적으로 정렬되지 않은 데이터 유형으로 정의 된 set을 반환합니다. 즉, 세트의 요소는 임의의 순서로 리턴 될 수 있습니다. 세트는 회원 테스트를 위해 최적화되어 있습니다 (if x in set:). 세트는 사전의 키와 같습니다. 어떤 순서로든 반복 할 수 있습니다.

return (a, b)가 그런 결과가 항상 같은 순서로 될 것입니다 : 당신이 튜플이 될 것이기

나는 더 나은 데이터 형식을 생각한다.

참고 문자 표기법의 차이 :

  • 사전 항목 : {'a': 'b', 'c': 'd')를 페어링 할 수 콜론이있다.
  • 세트가 콜론이 없으며, 단지 임의 주문 항목입니다 : {'a', 'b', 'c', 'd'}
  • 튜플에서 사용하는 괄호 : ('a', 'b', 'c', 'd')
  • 목록 대괄호를 사용하고 변경할 수 있습니다 : ['a', 'b', 'c', 'd']
+0

튜플은 일반적으로 괄호로 묶여 있지만 쉼표를 사용하면 튜플이되므로 'a', 'b', 'c', 'd''도 튜플이됩니다. 괄호는 빈 터플 또는 구문상의 이유로 필요한 경우를 제외하고는 선택 사항입니다 (예 : 쉼표가없는 함수의 인수 목록에서 쉼표로 인수를 구분할 수 있음). – Duncan

+0

@Duncan : 사실입니다. 또한 하나의 요소 튜플에 필요합니다. 쉼표는 구문의 일부가 아닌 경우 튜플을 생성한다고 말할 수 있습니다. – Max

+0

한 요소 튜플의 경우 괄호가 아니라 쉼표 만 필요합니다.'x = 1, '은 튜플을 생성합니다. – Duncan

관련 문제