【发布时间】:2016-02-22 14:53:28
【问题描述】:
假设我有一个列表,我想按以下模式对数字进行排序:
A = [3, 30, 34, 256, 5, 9]
先按个位数字排序,如果个位数字相同,我们将比较tens place,然后比较hundred place。如果您按此规则对 A 进行排序,则:
A = [9, 5, 34, 3, 30, 256]
9 is the highest digit at Unit place
5 is second highest
3, 34, 30 since unit digit is same here, we will compare tens place so 34 will come first here, then 3 and 30.
256 will come last since its unit place digit is 2 which is the lowest.
Suppose B = [100, 10, 1]
then after sorting B = [1, 10, 100]
谁能分享一些 Pythonic 的方法来解决这个问题?
我已经尝试过sorted(nums, key=lambda x: int(x[0]), reverse=True),但在这里我将如何考虑tenth place digit?
更新:假设A = [1, 100, 10] 那么在对A = [1, 10, 100] 进行排序之后的这种情况下,缺少一点。在示例中,我在对A = [9, 5, 34, 3, 30, 256] 进行排序后在这里给出了A = [3, 30, 34, 256, 5, 9]。
总体逻辑是我想加入所有数字并创建一个最大的 号码。
【问题讨论】:
-
单位是最右边的位置。 256的单位位置的数字是6。
-
天啊!我很累。让我重新表述一下这个问题:/
-
我的意思是从
leftmost to rightmost的位置询问。 -
[34, 3, 30] 是一个非常奇怪的选择。
-
主要思想是,如果将所有这些数字组合起来,它将生成可能的最大数字。
34330 > 34303
标签: python list python-2.7 sorting