简体   繁体   English

如何对数字中的数字进行排序?

[英]How to sort digits in a number?

I'm trying to make an easy script in Python which takes a number and saves in a variable, sorting the digits in ascending and descending orders and saving both in separate variables.我正在尝试在 Python 中制作一个简单的脚本,它接受一个数字并保存在一个变量中,按升序和降序对数字进行排序并将两者保存在单独的变量中。 Implementing Kaprekar's constant .实现Kaprekar 的常数

It's probably a pretty noobish question.这可能是一个非常菜鸟的问题。 But I'm new to this and I couldn't find anything on Google that could help me.但我是新手,我在谷歌上找不到任何可以帮助我的东西。 A site I found tried to explain a way using lists, but it didn't work out very well.我发现的一个网站试图解释使用列表的方法,但效果不佳。

Sort the digits in ascending and descending orders:按升序和降序对数字进行排序:

ascending = "".join(sorted(str(number)))

descending = "".join(sorted(str(number), reverse=True))

Like this:像这样:

>>> number = 5896
>>> ascending = "".join(sorted(str(number)))
>>>
>>> descending = "".join(sorted(str(number), reverse=True))
>>> ascending
'5689'
>>> descending
'9865'

And if you need them to be numbers again (not just strings), call int() on them:如果您需要它们再次成为数字(不仅仅是字符串),请对它们调用int()

>>> int(ascending)
5689
>>> int(descending)
9865

2020-01-30 2020-01-30

>>> def kaprekar(number):
...     diff = None
...     while diff != 0:
...         ascending = "".join(sorted(str(number)))
...         descending = "".join(sorted(str(number), reverse=True))
...         print(ascending, descending)
...         next_number = int(descending) - int(ascending)
...         diff = number - next_number
...         number = next_number
...
>>> kaprekar(2777)
2777 7772
4599 9954
3555 5553
1899 9981
0288 8820
2358 8532
1467 7641
>>> x = [4,5,81,5,28958,28] # first list
>>> print sorted(x)
[4, 5, 5, 28, 81, 28958]
>>> x
[4, 5, 81, 5, 28958, 28]
>>> x.sort() # sort the list in place
>>> x
[4, 5, 5, 28, 81, 28958]
>>> x.append(1) # add to the list
>>> x
[4, 5, 5, 28, 81, 28958, 1]
>>> sorted(x)
[1, 4, 5, 5, 28, 81, 28958]

As many others have pointed out, you can sort a number forwards like:正如许多其他人指出的那样,您可以对数字进行排序,例如:

>>> int(''.join(sorted(str(2314))))
1234

That's pretty much the most standard way.这几乎是最标准的方式。

Reverse a number?反转一个数字? Doesn't work well in a number with trailing zeros.在带有尾随零的数字中效果不佳。

>>> y = int(''.join(sorted(str(2314))))
>>> y
1234
>>> int(str(y)[::-1])
4321

The [::-1] notation indicates that the iterable is to be traversed in reverse order. [::-1]表示法表示以相反的顺序遍历可迭代对象。

As Mark Rushakoff already mentioned (but didn't solve) in his answer, str(n) doesn't handle numeric n with leading zeros, which you need for Kaprekar's operation .正如 Mark Rushakoff 在他的回答中已经提到(但没有解决)的那样, str(n)不处理带有前导零的数字n ,这是Kaprekar 的操作所需要 hughdbrown's answer similarly doesn't work with leading zeros.休德布朗的回答同样不适用于前导零。

One way to make sure you have a four-character string is to use the zfill string method.确保您拥有四字符字符串的一种方法是使用zfill字符串方法。 For example:例如:

>>> n = 2
>>> str(n)
'2'
>>> str(n).zfill(4)
'0002'

You should also be aware that in versions of Python prior to 3, a leading zero in a numeric literal indicated octal:您还应该知道,在 Python 3 之前的版本中,数字文字中的前导零表示八进制:

>>> str(0043)
'35'
>>> str(0378)
  File "<stdin>", line 1
    str(0378)
           ^
SyntaxError: invalid token

In Python 3, 0043 is not a valid numeric literal at all.在 Python 3 中, 0043根本不是有效的数字文字。

我不知道 python 语法,但一般认为,我会将输入字符串转换为字符数组,他们对字符数组进行排序,最后将其输出。

Here's an answer to the title question in Perl, with a bias toward sorting 4-digit numbers for the Kaprekar algorithm.这是 Perl 中标题问题的答案,偏向于为 Kaprekar 算法排序 4 位数字。 In the example, replace 'shift' with the number to sort.在示例中,将 'shift' 替换为要排序的数字。 It sorts digits in a 4-digit number with leading 0's ($asc is sorted in ascending order, $dec is descending), and outputs a number with leading 0's:它对前导 0 的 4 位数字中的数字进行排序($asc 按升序排序,$dec 降序),并输出前导为 0 的数字:

my $num = sprintf("%04d", shift);
my $asc = sprintf("%04d", join('', sort {$a <=> $b} split('', $num)));
my $dec = sprintf("%04d", join('', sort {$b <=> $a} split('', $num)));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM