【问题标题】:Sort a list based on unit place, tens place, hundred place digit in Python在Python中根据个位,十位,百位数字对列表进行排序
【发布时间】: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


【解决方案1】:

我认为你只需要 str 作为键:

In [11]: sorted(A, key=str, reverse=True)
Out[11]: [9, 5, 34, 30, 3, 256]

最初我读到你的问题是你想要颠倒的数字:

In [12]: sorted(A, key=lambda x: str(x)[::-1])
Out[12]: [30, 3, 34, 5, 256, 9]

【讨论】:

  • 如果有像A = [3, 30, 300] 这样的三个数字,那么30 应该先出现3,30 应该在300 之前出现。
  • 我想你想要sorted(A, key=lambda x: str(x) + "a", reverse=True),但从你的例子中有点不清楚。
  • 主要思想是,如果将所有这些数字结合起来,它应该会生成一个最大的数字。所以如果你结合A = [9, 5, 34, 3, 30, 256] 9534330256 是你能得到的最大数。
【解决方案2】:

以下代码回答了更新后的问题:“以一种连接排序后的数字将给出可能的最高数字的方式进行排序”。 这个想法是,如果最重要的数字相同并且长度不同,则如果在较长的数字中(较短的数字长度 + 1)数字大于或等于最高有效数字,则较长的数字比较短的数字“更大”。例如:30 3、10 3、3001 34、342

>>> def f(x, y):
...     if x == y:
...         return 0
...     xs = str(x)
...     ys = str(y)
...     for i in range(min(len(xs), len(ys))):
...         if xs[i] > ys[i]:
...             return 1
...         elif xs[i] < ys[i]:
...             return -1
...     if len(xs) > len(ys):
...         return 1 if xs[0] <= xs[len(ys)] else -1
...     return -1 if ys[0] <= ys[len(xs)] else 1
...
>>> A = [3, 30, 34, 256, 5, 9]
>>> B = [100,10,1]
>>> sorted(A, cmp=f, reverse=True)
[9, 5, 34, 3, 30, 256]
>>> sorted(B, cmp=f, reverse=True)
[1, 10, 100]

【讨论】:

    【解决方案3】:

    哦,您确实想要数字作为文本的字符串排序。好吧,如果您想要您描述的单位 -> 数十 -> 数百排序,就可以了:

    # Repeatedly modulo 10 to get the rightmost digit
    # (units, then tens, then hundreds) until the 
    # digit where the two numbers differ. Compare those two digits.
    >>> def f(x, y):
    ...     xr = x % 10
    ...     yr = y % 10
    ...     while x and y and xr == yr:
    ...         x, xr = divmod(x, 10)
    ...         y, yr = divmod(y, 10)
    ...     return cmp(xr, yr)
    ... 
    >>> A = [3, 30, 34, 256, 5, 9]
    >>> sorted(A, cmp=f)
    [30, 3, 34, 5, 256, 9]
    

    它不像您的示例输出那样排序,而是按单位排序 - 0、3、4、5、6、9。如果有任何单位相同的地方,它按十排序,等等。

    >>> A = [3, 30, 34, 266, 256, 5, 9]
    >>> sorted(A, cmp=f)
    [30, 3, 34, 5, 256, 266, 9]
    

    【讨论】:

    • cmp 在 python 3 中不可用。:(
    • @AndyHayden 可耻。它可能被替换为一些在repl 中使用很糟糕的东西,就像其他所有东西一样,是吗?幸运的是,这个问题被标记为 Python-2.7。 (不是我之前注意到的)。
    【解决方案4】:

    这是与之前发布的其他答案不同的版本:

    import itertools
    
    def produce_list(A):
       At = [str(x) for x in A]  # convert the items to str so we can join them
       result = 0
       for i in itertools.permutations(At):  # go through all permutations
          temp = int(''.join(i))
          if result < temp:   # find the biggest value of the combined numbers
              result = temp
              result_list = list(i)
    
       return [int(x) for x in result_list]  # return the list of ints
    
    print produce_list([3, 30, 34, 256, 5, 9])
    print produce_list([100, 1, 10])
    

    它可能不是很有效(它会通过每一种组合),但它是我能得到的 Pythonic。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      • 2016-08-03
      相关资源
      最近更新 更多