“[1:][::-1]” 的意思是什么,例如 (str) a[1:][::-1]?

huangapple go评论98阅读模式
英文:

What does "[1:][::-1]" mean, for example (str) a[1:][::-1]?

问题

这实际上是LeetCode上的一个问题,我在解决方案中找到了这部分代码。但我不明白这部分代码是如何工作的。问题基本上是反转给定的整数,例如:x = 123,那么结果应该返回 321。
这是代码:

class Solution:
    def reverse(self, x: int) -> int:

    s = str(x)

    def check(m):
        if ans > 4294967295/2 or ans < -4294967295/2:
            return 0
        else: 
            return ans


    if x < 0:
        ans = int('-' + s[1:][::-1])
        return check(ans)

    else:
        ans = int(s[::-1])
        return check(ans)

我是编程的初学者,所以我从未在Python字符串中见过类似的东西。

英文:

This is actually a problem on leetcode, I found this part of code in the solution. But I couldn't understand how does this part of code work. The problem was just basically reverse a given integer, for example: x = 123, then the result should return 321.
This is the code:

class Solution:
    def reverse(self, x: int) -&gt; int:

    s = str(x)

    def check(m):
        if ans &gt; 4294967295/2 or ans &lt; -4294967295/2:
            return 0
        else: 
            return ans
    
    
    if x &lt; 0:
        ans = int(&#39;-&#39; + s[1:][::-1])
        return check(ans)
        
    else:
        ans = int(s[::-1])
        return check(ans)

I'm a beginner in programming so I have never seen anything like that in a string in python.

答案1

得分: 2

这些是Python字符串切片

字符串切片有三个部分 - 起始位置、结束位置和步长。它们采用以下形式:string_obj[start_position:end_position:step]

当省略时,起始位置将默认为字符串的开头,结束位置将默认为字符串的末尾,步长将默认为1。

表达式s[1:][::-1]执行了两次切片操作。

s[1:] 使用索引1作为起始位置,将结束位置留空(默认为字符串的末尾)。它返回一个与s相同的字符串,只是没有字符串的第一个字符。

然后,这个新字符串被传递到第二次切片操作中。[::-1] 没有定义起始位置或结束位置,因此将使用整个字符串,并且步长位置的-1意味着返回的字符串将以每次一步向后遍历,即颠倒字符串。

英文:

These are Python string slices.

There are three parts to a string slice - the starting position, the end position and the step. These take the form string_obj[start_position:end_position:step].

When omitted the start position will default to the start of the string, then end position will default to the end of the string, and the step will default to 1.

The expression s[1:][::-1] is doing two slice operations.

s[1:] uses index 1 as the start position and leaves the end position blank (which defaults to the end of the string). It returns a string that's the same as s only without the first character of the string.

This new string is then fed into the second slicing operation. [::-1] has no start or end position defined so the whole string will be used, and the -1 in the step place means that the returned string will be stepped through backwards one step at a time, reversing the string.

答案2

得分: 0

I will provide translations for the code portion as requested:

我将提供代码部分的翻译如下所示

I will explain with an example
我将以一个示例来解释

x = 123
s = str(x) #index will be 0, 1, 2
x = 123
s = str(x) #索引将为0, 1, 2

print(s[1:])
print(s[1:])
This will print **23** as the output. ie, The index starts from 0, when you put **s[1:]** the index starts from 1 and goes till the end gets printed out.
这将打印出**23**作为输出也就是说索引从0开始当你使用**s[1:]**索引从1开始直到末尾被打印出来

**s[::-1]** will reverse the string. &quot;**:**&quot; means starting from the beginning and going till the ending &quot;**:**&quot; with a step of **-1**. This means that it will start from the ending and goes to the start as the step is **-1**.
**s[::-1]**将反转字符串&quot;**:**&quot;表示从开头开始直到末尾并以步长**-1**前进这意味着它将从末尾开始并向前移动因为步长为**-1**

In this particular problem, we need to reverse the integer. When the number becomes less than zero. **-123** when reversed using string ie, **s[::-1]** gets converted to **321-**. In order to reverse the negative integer as **-321**, **s[1:]** is used to remove &quot;-&quot; and converting the **123** to **321**
在这个特定问题中我们需要反转整数当数字变小于零时**-123**在使用字符串反转时**s[::-1]**被转换为**321-**为了将负整数反转为**-321**使用**s[1:]**来移除&quot;-&quot;并将**123**转换为**321**

ans = int(&#39;-&#39; + s[1:][::-1])
ans = int(&#39;-&#39; + s[1:][::-1])
**When the integer value is less than 0.**
**当整数值小于0时**

The above line of code converts the **-123** to **-321**. &quot;-&quot; gets neglected when reversing the string and gets added up when `&#39;-&#39; + s[1:][::-1]` this string operation works. Taking int() the string will convert **&quot;-321&quot;** to **-321**, ie, string to integer form.
上面的代码将**-123**转换为**-321**在反转字符串时&quot;-&quot;被忽略当执行`&#39;-&#39; + s[1:][::-1]`这个字符串操作时,它会被添加。使用int()将字符串转换为**&quot;-321&quot;**将其转换为**-321**,即字符串转换为整数形式。

**When the integer value is above 0**
**当整数值大于0时**

def check(m):
def check(m):
    if ans &gt; 4294967295/2 or ans &lt; -4294967295/2:
    if ans &gt; 4294967295/2 or ans &lt; -4294967295/2:
        return 0
        return 0
    else:
    else:
        return ans
        return ans
This is a constraint given in the problem that the values of the reversed integer should be between **-2^31** and **2^31** otherwise it should be returned **0**.
这是问题中提出的限制条件反转后的整数值应该在**-2^31****2^31**之间否则应返回**0**
英文:

I will explain with an example

x = 123 
s = str(x) #index will be 0, 1, 2
print(s[1:])

This will print 23 as the output. ie, The index starts from 0, when you put s[1:] the index starts from 1 and goes till the end gets printed out.

s[::-1] will reverse the string. ":" means starting from the beginning and going till the ending ":" with a step of -1. This means that it will start from the ending and goes to the start as the step is -1.

In this particular problem, we need to reverse the integer. When the number becomes less than zero. -123 when reversed using string ie, s[::-1] gets converted to 321-. In order to reverse the negative integer as -321, s[1:] is used to remove "-" and converting the 123 to 321

ans = int(&#39;-&#39; + s[1:][::-1])

When the integer value is less than 0.

The above line of code converts the -123 to -321. "-" gets neglected when reversing the string and gets added up when &#39;-&#39; + s[1:][::-1] this string operation works. Taking int() the string will convert "-321" to -321, ie, string to integer form.

When the integer value is above 0
We only need to reverse the integer using s[::-1]

def check(m):
if ans &gt; 4294967295/2 or ans &lt; -4294967295/2:
return 0
else: 
return ans

This is a constraint given in the problem that the values of the reversed integer should be between -2^31 and 2^31 otherwise it should be returned 0.

huangapple
  • 本文由 发表于 2023年1月6日 12:51:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75027048.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定