python 如何对数字中的数字进行排序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1301156/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to sort digits in a number?
提问by bleakgadfly
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. Implementing Kaprekar's constant.
我正在尝试在 Python 中制作一个简单的脚本,它接受一个数字并保存在一个变量中,按升序和降序对数字进行排序并将两者保存在单独的变量中。实现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.
这可能是一个非常菜鸟的问题。但我是新手,我在谷歌上找不到任何可以帮助我的东西。我发现的一个网站试图解释使用列表的方法,但效果不佳。
回答by hughdbrown
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
回答by Mark Rushakoff
>>> 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]
符号表示以相反的顺序遍历可迭代对象。
回答by John Y
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. hughdbrown's answer similarly doesn't work with leading zeros.
正如 Mark Rushakoff 在他的回答中已经提到的(但没有解决),str(n)
不处理n
带有前导零的数字,这是Kaprekar 的操作所需要的。休德布朗的回答同样不适用于前导零。
One way to make sure you have a four-character string is to use the zfill
string method. For example:
确保您拥有四字符字符串的zfill
一种方法是使用string 方法。例如:
>>> 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
根本不是有效的数字文字。
回答by Jay
I don't know the python syntax, but thinking the generically, I would convert the input string into a character array, they do a sort on the character array, and lastly pipe it out.
我不知道 python 语法,但一般认为,我会将输入字符串转换为字符数组,他们对字符数组进行排序,最后将其输出。
回答by indiv
Here's an answer to the title question in Perl, with a bias toward sorting 4-digit numbers for the Kaprekar algorithm. In the example, replace 'shift' with the number to sort. 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:
这是 Perl 中标题问题的答案,偏向于为 Kaprekar 算法排序 4 位数字。在示例中,将 'shift' 替换为要排序的数字。它对前导 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)));