V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
zeroday
V2EX  ›  问与答

在看 Think Python,有一道练习题没看明白,能帮忙翻译一下吗?

  •  
  •   zeroday · 2015-02-08 12:05:25 +08:00 · 2418 次点击
    这是一个创建于 3426 天前的主题,其中的信息可能已经有所发展或是发生改变。

    Exercise 12.2. In this example, ties are broken by comparing words, so words with the same length appear in reverse alphabetical order.

    For other applications you might want to break ties at random.

    Modify this example so that words with the same length appear in random order.

    Hint: see the random function in the random module.

    4 条回复    2015-02-08 13:19:24 +08:00
    manoon
        1
    manoon  
       2015-02-08 12:13:03 +08:00 via Android   ❤️ 1
    words with the same length appear in random order.

    Hint: see the random function in the random module.


    上一节是练习使用升序/降序

    这一节,你就先找出len相同的words,然后随机排序就行啦。

    (愚见,仅供参考)
    zeroday
        2
    zeroday  
    OP
       2015-02-08 12:33:32 +08:00
    @manoon 谢谢,不理解 ties are broken by comparing words 和For other applications you might want to break ties at random. 主要是不理解 ties break 的意思。
    zeroday
        3
    zeroday  
    OP
       2015-02-08 12:55:02 +08:00
    @manoon 奇怪的是,上一节练习的例子是根据列表中单词的长度,进行降序排列,代码是这样的。

    https://gist.github.com/arcticlion/f75f89fd1c0ff4ef1cde

    运行结果是这样的:

    wooooo
    applee
    Harlod
    Finich
    world
    tuple
    hello
    apple
    nba
    ibm

    对于相同的长度的单词,打印出现的顺序本来就是随机。
    zeroday
        4
    zeroday  
    OP
       2015-02-08 13:19:24 +08:00
    突然明白了,上一例在相同长度上的输出虽然不是按字母表顺序,但是每次输出都是固定的。练习需要我修改上一例的代码,让每次在相同长度上的输出是不固定的。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2851 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 14:12 · PVG 22:12 · LAX 07:12 · JFK 10:12
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.