2012-08-02 56 views
2

我使用這個代碼:相同的函數在Python中以相反順序給出不同的結果。爲什麼?

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

要運行一些測試,我使用的代碼使用上面的功能:

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

這是結果:

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.']) 

正如你所看到的,有時我得到了從主機應用程序複製之前的狀態信息 - 像第一行。

但有時我會得到狀態信息之前複製的信息,如第二行。

熟悉使用dict返回信息,所以我想這可能是一個問題,特別是當與事實我想返回兩個變量混合。

任何人都可以解釋爲什麼會發生這種情況?

我知道我可以簡單地使用dict並在返回之前將返回信息保存到變量中,但我真的認爲這是一個更優雅的解決方案 - 我錯了嗎?

回答

6

set s是無序的(或更好的,它們的順序是任意的)。除了使用有序的數據類型外,你無能爲力。

例如通過去除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 2012-08-02 16:37:38

+0

在這種情況下通常要做的事情是返回一個元組。 – 2012-08-02 16:37:56

+0

@MarkRansom:當'set'構造函數被移除時,這裏發生了什麼。 – 2012-08-02 16:38:38

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''也是一個元組。括號是可選的,除了空元組或者由於句法原因需要它們的地方(例如,在沒有parens的函數的參數列表中,逗號將分隔參數)。 – Duncan 2012-08-02 17:04:34

+0

@鄧肯:的確如此。它們也是一個元素元組所必需的。人們可以說,當它們不是語法的一部分時,逗號就會創建元組。 – Max 2012-08-02 17:09:17

+0

對於一個元素元組,您只需要逗號,而不是括號:'x = 1,'將創建一個元組。 – Duncan 2012-08-03 17:23:37

相關問題