如何在python中反转int?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24953303/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 05:30:42  来源:igfitidea点击:

How to reverse an int in python?

pythonreverse

提问by Matthew C

I'm creating a python script which prints out the whole song of '99 bottles of beer', but reversed. The only thing I cannot reverse is the numbers, being integers, not strings.

我正在创建一个 python 脚本,它打印出“99 瓶啤酒”的整首歌曲,但反过来了。我唯一不能反转的是数字,是整数,而不是字符串。

This is my full script,

这是我的完整剧本

def reverse(str):
   return str[::-1]

def plural(word, b):
    if b != 1:
        return word + 's'
    else:
        return word

def line(b, ending):
    print b or reverse('No more'), plural(reverse('bottle'), b), reverse(ending)

for i in range(99, 0, -1):
    line(i, "of beer on the wall")
    line(i, "of beer"
    print reverse("Take one down, pass it around")
    line(i-1, "of beer on the wall \n")

I understand my reverse function takes a string as an argument, however I do not know how to take in an integer, or , how to reverse the integer later on in the script.

我知道我的 reverse 函数将一个字符串作为参数,但是我不知道如何接收一个整数,或者,如何在脚本中稍后反转这个整数。

采纳答案by jonrsharpe

You are approaching this in quite an odd way. You already have a reversing function, so why not make linejust build the line the normal way around?

你正在以一种非常奇怪的方式接近这个。你已经有了一个倒车功能,那么为什么不按照正常的方式line建造这条线呢

def line(bottles, ending):
    return "{0} {1} {2}".format(bottles, 
                                plural("bottle", bottles), 
                                ending)

Which runs like:

运行如下:

>>> line(49, "of beer on the wall")
'49 bottles of beer on the wall'

Then pass the result to reverse:

然后将结果传递给reverse

>>> reverse(line(49, "of beer on the wall"))
'llaw eht no reeb fo selttob 94'

This makes it much easier to test each part of the code separately and see what's going on when you put it all together.

这使得单独测试代码的每个部分并查看将它们放在一起时会发生什么变得更加容易。

回答by gefei

Something like this?

像这样的东西?

>>> x = 123
>>> str(x)
'123'
>>> str(x)[::-1]
'321'

回答by Alberto

Without converting the number to a string:

不将数字转换为字符串:

def reverse_number(n):
    r = 0
    while n > 0:
        r *= 10
        r += n % 10
        n /= 10
    return r

print(reverse_number(123))

回答by Amduscias

You can cast an integer to string with str(i) and then use your reverse function.

您可以使用 str(i) 将整数转换为字符串,然后使用您的反向函数。

The following line should do what you are looking for:

以下行应该做你正在寻找的:

    def line(b, ending):
        print reverse(str(b)) or reverse('No more'), plural(reverse('bottle'),reverse(str(b))), reverse(ending)

回答by Divyank

Original number is taken in a

原始号码是在一个

a = 123

We convert the int to string ,then reverse it and again convert in int and store reversed number in b

我们将 int 转换为 string ,然后将其反转并再次转换为 int 并将反转后的数字存储在 b 中

b = int("".join(reversed(str(a))))

b = int("".join(reversed(str(a))))

Print the values of a and b print(a,b)

打印 a 和 b 的值 print(a,b)

回答by Satyamskillz IN

best way is

最好的办法是

x=12345
a=str(x)[::-1]\ In this process i have create string of inverse of integer (a="54321")
a=int(a) \ Here i have converted string a in integer 

or one line code is

或一行代码是

a=int(str(x)[::-1]))

回答by srujan kumar

def reverse_number(n):
r = 0
while n > 0:
    r *= 10
    r += n % 10
    n /= 10
return r

print(reverse_number(123))

打印(反向编号(123))

This code will not work if the number ends with zeros, example 100 and 1000 return 1

如果数字以零结尾,此代码将不起作用,例如 100 和 1000 返回 1

回答by Vinay

def reverse_number(n):
r = 0
while n > 0:
    r = (r*10) + (n % 10)
    print(r)
    r *=10
    n //= 10
return r

print(reverse_number(123))

回答by Anuj shah

I have written it in a different way, but it works

我用不同的方式写了它,但它有效

def isPalindrome(x: int) -> bool:

    if x<0:
        return False
    elif x<10:
        return True
    else:
        rev=0
        rem = x%10
        quot = x//10
        rev = rev*10+rem
        while (quot>=10):
           rem = quot%10
           quot = quot//10
           rev = rev*10+rem
        rev = rev*10+quot
        if rev==x:
            return True
        else:
            return False

res=isPalindrome(1221)