“int(a[::-1])"是什么意思?在 Python 中? [英] What is the meaning of "int(a[::-1])" in Python?
本文介绍了“int(a[::-1])"是什么意思?在 Python 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我无法理解这一点.我在人们的代码中看到了这一点.但无法弄清楚它的作用.这是在 Python 中.
str(int(a[::-1]))
解决方案
假设 a
是一个字符串.python 中的 Slice 符号具有语法 -
list[::]
因此,当您执行 a[::-1]
时,它会从末尾开始向第一个获取每个元素.所以它反转了a.这也适用于列表/元组.
示例 -
<预><代码>>>>一个 = '1234'>>>[::-1]'4321'然后你把它转换成 int 然后再转换回字符串(虽然不知道你为什么这样做),这只会给你返回字符串.
I cannot understand this. I have seen this in people's code. But cannot figure out what it does. This is in Python.
str(int(a[::-1]))
解决方案
Assuming a
is a string. The Slice notation in python has the syntax -
list[<start>:<stop>:<step>]
So, when you do a[::-1]
, it starts from the end towards the first taking each element. So it reverses a. This is applicable for lists/tuples as well.
Example -
>>> a = '1234'
>>> a[::-1]
'4321'
Then you convert it to int and then back to string (Though not sure why you do that) , that just gives you back the string.
这篇关于“int(a[::-1])"是什么意思?在 Python 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文