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

2015-02-08 12:05:25 +08:00
 zeroday

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.

2419 次点击
所在节点    问与答
4 条回复
manoon
2015-02-08 12:13:03 +08:00
words with the same length appear in random order.

Hint: see the random function in the random module.


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

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

(愚见,仅供参考)
zeroday
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
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
2015-02-08 13:19:24 +08:00
突然明白了,上一例在相同长度上的输出虽然不是按字母表顺序,但是每次输出都是固定的。练习需要我修改上一例的代码,让每次在相同长度上的输出是不固定的。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/169227

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX