首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Python中,相同的函数以相反的顺序给出不同的结果。为什么?

在Python中,相同的函数以相反的顺序给出不同的结果。为什么?
EN

Stack Overflow用户
提问于 2012-08-02 16:32:38
回答 2查看 332关注 0票数 2

我用的是这个代码:

代码语言:javascript
复制
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。

为了运行一些测试,我使用了以下代码,该代码使用了上面的函数:

代码语言:javascript
复制
for a in range(15,22):
    print copy_part_of_space(a,7,8)

这是结果:

代码语言:javascript
复制
   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并在传递返回之前将返回信息保存到一个变量中,但是我真的认为这是一个更优雅的解决方案--我错了吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-08-02 16:33:52

set是无序的(更好的是,它们的顺序是任意的)。除了使用有序的数据类型之外,您什么也做不了。

例如,通过删除set构造函数{...}

代码语言:javascript
复制
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

现在,这段代码返回一个元组 (第一个元素是来自“错误消息字典”的查找结果,第二个元素包含在data_string.value中)。

票数 6
EN

Stack Overflow用户

发布于 2012-08-02 16:39:04

您将具体返回一个set,它被定义为一个无序的数据类型。也就是说,集合的元素可以按任何顺序返回。集合是为成员资格测试(if x in set:)优化的。集合就像字典中的键:它们可以按任何顺序迭代。

我认为更好的数据类型应该是元组:return (a, b)

那么,结果将始终是相同的顺序。

注意文字表示法的差异:

  • 字典使用冒号对项。:{'a': 'b', 'c': 'd')
  • Set没有冒号,只是任意排序的项:{'a', 'b', 'c', 'd'}
  • 元组使用括号:('a', 'b', 'c', 'd')
  • 列表使用方括号,并且是可变的:['a', 'b', 'c', 'd']
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11781939

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档