python索引切片

    科技2022-07-12  137

    python索引切片

    Beginner guide to understanding Python’s slice object.

    理解Python的slice对象的入门指南。

    If you’re unsure about how Python indexes and slices strings, this article is for you! First, let’s explore indexes. (Indices for the grammatically correct!).

    如果您不确定Python如何索引和切片字符串,那么本文适合您! 首先,让我们探索索引。 ( 指数为语法正确的!)。

    This is a companion article to my beginner series:

    这是我的初学者系列的配套文章:

    Python使用基于零的索引 (Python Uses Zero-Based Indexes)

    The first element in a sequence has the index 0. Not 1. This is often a source of confusion!

    序列中的第一个元素的索引为0。不是1。这通常是造成混乱的原因!

    Although the first character has the index 0, and the last character has index 11, there are in fact 12 characters in this string.

    尽管第一个字符的索引为0,最后一个字符的索引为11,但实际上该字符串中有12个字符。

    使用方括号访问索引 (Access an Index using Square Brackets)

    The character at index position 6 is ‘W’.

    索引位置6处的字符是'W' 。

    使用负索引号 (Using Negative Index Numbers)

    Going forwards from 0, W is at index position 6. Going backwards from -1, W is at index position -6.

    从0向前, W在索引位置6。从-1向后, W在索引位置-6。

    The negative index begins at -1, not zero. Why? Zero is already used for the first character in the forward index!

    负索引从-1开始,而不是零。 为什么? 零已用于正向索引中的第一个字符!

    枚举字符序列 (Enumerating The Sequence of Characters)

    Python has a very handy enumerate() method. It gives us each element of the character sequence, and the index starting at 0.

    Python有一个非常方便的enumerate()方法。 它为我们提供了字符序列的每个元素,索引从0开始。

    This prints out the following in my terminal:

    这会在我的终端中打印出以下内容:

    [(0, ‘H’), (1, ‘e’), (2, ‘l’), (3, ‘l’), (4, ‘o’), (5, ‘ ‘), (6, ‘W’), (7, ‘o’), (8, ‘r’), (9, ‘l’), (10, ‘d’), (11, ‘!’)]

    This corresponds nicely to the first image, above.

    这很好地对应于上面的第一个图像。

    Python的IndexError (Python’s IndexError)

    The character at index position 12 is… not there at all!

    索引位置12处的字符根本不存在!

    Trying to access an index position which is outside of the bounds raises an error:

    尝试访问超出范围的索引位置会引发错误:

    IndexError: string index out of range

    字符串是不可变的 (Strings are Immutable)

    You might want to change a letter. Say to change W here to an L. Not in Python! The string is immutable — it can’t be changed.

    您可能要换一个字母。 说这里将W更改为L 不在Python中! 该字符串是不可变的-不能更改。

    Instead, we get a TypeError error:

    相反,我们收到TypeError错误:

    TypeError: 'str' object does not support item assignment

    一片馅饼 (A Slice of Pie)

    We can get a slice of the string sequence by using slice notation. The slice is given by values inside the square brackets:

    我们可以使用切片符号来获取字符串序列的切片 。 切片由方括号内的值给出:

    [ start_at : stop_before : step ]

    The step value is optional and can be left out.

    step值是可选的,可以省略。

    To retrieve the text “lo Wo”, using the information above, we need:

    要使用上面的信息来检索文本“ lo Wo”,我们需要:

    start_at = 3stop_before = 8step = nothing

    Let’s try this in Python:

    让我们在Python中尝试一下:

    “剩下的所有馅饼”切片 (The “All The Pie Remaining” Slice)

    Let’s say we have pie. Real pie. I want you to have the first 5 slices. I want all the remaining slices, 6 to whatever else is left.

    假设我们有馅饼。 真正的馅饼。 我希望你能拿到前5片。 我希望所有剩余切片,6到任何其他被留下。

    start_at = 6stop_before = :step = nothing

    The notation is:

    表示法是:

    [6:]

    This means: start at 6, continue to the end.

    这意味着:从6开始,继续到结束。

    馅饼的开始! (The Start of the Pie Only!)

    Let’s be less greedy! Let’s just take the first 5 slices of pie:

    让我们少一点贪心吧! 让我们只拿前 5片馅饼:

    This means we stop before index 5! Remember, slice at index 0 is also a slice!

    这意味着我们在索引5 之前停止 ! 请记住,索引0处的片也是片!

    The notation is:

    表示法是:

    [:5]

    所有的馅饼! (All The Pie!)

    There is one final special thing to note about the slice syntax, the *everything* construct:

    关于切片语法,需要注意的最后一件事是“ 一切”结构:

    [:]

    This slice captures everything in the sequence!

    此切片将捕获序列中的所有内容!

    在切片中使用负索引 (Using Negative Indexes in The Slice)

    To get World (without the final !)we could go forwards:

    要获得World (没有决赛! ),我们可以前进:

    [6:11]

    Or we could use the negative index values, too:

    或者我们也可以使用负索引值:

    [-6:-1]

    向后退一步 (Going Backwards with a Negative Step)

    Let’s say we want to eat our pie slice World backwards, like this: dlroW. We could use the positive index values and use a step of -1. Remember the slice syntax:

    假设我们想向后吃我们的馅饼World ,就像这样: dlroW 。 我们可以使用正指数值并使用-1步长。 记住切片语法:

    [ start_at : stop_before : step ]start_at = index valuestop_before = index valuestep = step value

    We need to use:

    我们需要使用:

    [10:5:-1]

    Which gives us:

    这给了我们:

    'dlroW'

    混合和匹配负索引值和正索引值 (Mix and Match Negative and Positive Index Values)

    This is getting complicated now! And quite crazy!

    现在这变得越来越复杂! 而且很疯狂!

    We’re still interested in retrieving the string dlroW.

    我们仍然对检索字符串dlroW 。

    [ start_at : stop_before : step ]start_at = index valuestop_before = index valuestep = step value

    Earlier we used:

    之前我们使用了:

    [10:5:-1]

    But of course, we can start at 10 or at -2, it’s the same position d!

    但是,当然,我们可以从10或-2 ,这是相同的位置d !

    [-2:5:-1]

    And of course, we can choose to stop_before either the positive index value of 5, or the negative index value of -7.

    当然,我们可以选择stop_before在正索引值5或负索引值-7 。

    Python3中的Slice对象 (The Slice Object in Python3)

    We’ve been using literal values so far. Actually typing them out, like [-2:5:-1] . But! Python has the slice object which we can use.

    到目前为止,我们一直在使用文字值。 实际输入它们,例如[-2:5:-1] 。 但! Python具有我们可以使用的slice对象。

    Which results in:

    结果是:

    Start: -2Stop: 5Step: -1Slice: slice(-2, 5, -1)dlroW

    I hope you enjoyed this! Check out my article talking about intermediate level string tips and tricks too :)

    希望您喜欢! 看看我的文章,也谈论中级字符串的提示和技巧:)

    升级编码 (Level Up Coding)

    Thanks for being a part of our community! Subscribe to our YouTube channel or join the Skilled.dev coding interview course.

    感谢您加入我们的社区! 订阅我们的YouTube频道或参加Skilled.dev编码面试课程 。

    翻译自: https://levelup.gitconnected.com/a-visual-guide-to-python-slices-and-indexes-507af00b0c45

    python索引切片

    相关资源:微信小程序源码-合集6.rar
    Processed: 0.015, SQL: 8