V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
zeroday
V2EX  ›  问与答

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

  •  
  •   zeroday · Feb 8, 2015 · 2762 views
    This topic created in 4102 days ago, the information mentioned may be changed or developed.

    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 replies    2015-02-08 13:19:24 +08:00
    manoon
        1
    manoon  
       Feb 8, 2015 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
       Feb 8, 2015
    @manoon 谢谢,不理解 ties are broken by comparing words 和For other applications you might want to break ties at random. 主要是不理解 ties break 的意思。
    zeroday
        3
    zeroday  
    OP
       Feb 8, 2015
    @manoon 奇怪的是,上一节练习的例子是根据列表中单词的长度,进行降序排列,代码是这样的。

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

    运行结果是这样的:

    wooooo
    applee
    Harlod
    Finich
    world
    tuple
    hello
    apple
    nba
    ibm

    对于相同的长度的单词,打印出现的顺序本来就是随机。
    zeroday
        4
    zeroday  
    OP
       Feb 8, 2015
    突然明白了,上一例在相同长度上的输出虽然不是按字母表顺序,但是每次输出都是固定的。练习需要我修改上一例的代码,让每次在相同长度上的输出是不固定的。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   806 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 21:57 · PVG 05:57 · LAX 14:57 · JFK 17:57
    ♥ Do have faith in what you're doing.