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 を使用) を返す必要があります。

いくつかのテストを実行するために、上記の関数を使用するこのコードを使用していました。

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

ご覧のとおり、最初の行のように、ホスト アプリケーションからコピーされる前にステータス情報を取得することがあります。

しかし、2 行目のように、ステータス情報の前にコピーされた情報を取得することがあります。

私は情報を返すために使用することに慣れていないdictので、特に2つの変数を返そうとしているという事実と混ざり合ったときに、それが問題になる可能性があると思います.

なぜこれが起こっているのか誰でも説明できますか?

dictリターンを渡す前に単純に を使用してリターン情報を変数に保存できることはわかっていますが、これはより洗練されたソリューションだと本当に思っていました-間違っていますか?

4

2 に答える 2

6

setsは順序付けられていません(または、より適切には、順序は任意です)。代わりに順序付きデータ型を使用する以外に、これについてできることはありません。

たとえば、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

このコードは代わりにタプルを返します(最初の要素は「エラーメッセージディクショナリ」からのルックアップ結果であり、2番目の要素は含まれているものは何でもですdata_string.value)。

于 2012-08-02T16:33:52.147 に答える
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']
于 2012-08-02T16:39:04.713 に答える