美文网首页大数据 爬虫Python AI SqlPython学习分享程序员
十八问,认识Python序列,解决疑难杂症!

十八问,认识Python序列,解决疑难杂症!

作者: 烟雨丿丶蓝 | 来源:发表于2019-06-02 14:02 被阅读11次

    序列是Python中的重要数据结构,序列包括字符串,列表,元组。

    大部分朋友学习Python的时候都会找本书或者资料从头看到尾,这次我们换一个思路,问答式的方式,可能让我们精力更集中,下面开始我们的提问:

    1.什么是序列?

    序列是将元素按照顺序排列,通过索引(下标)访问;

    2.能直观描述下吗?

    直接上图:

    image

    序列中的每个元素按顺序排列,使用索引进行访问,索引分为正负索引;

    3.具体如何访问,正负索引是不是有点多余?

    举一个栗子,字符串:msg ='helloworld',在jupyter下操作如下:

    <pre class="prettyprint hljs bash" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">msg = 'helloworld'

    Python学习交流群:835017344,这里是python学习者聚集地,有大牛答疑,有资源共享!有想学习python编程的,或是转行,或是大学生,还有工作中想提升自己能力的,正在学习的小伙伴欢迎加入学习。


    获取第一个元素

    print(msg[0])

    获取最后一个?

    print(msg[-1])

    获取第3个元素

    print(msg[2])

    获取第12个元素

    print(msg[11])</pre>

    输出结果:

    <pre class="prettyprint hljs sql" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">h d l

    IndexError Traceback (most recent call last)
    <ipython-input-6-589026671881> in <module>()
    7 print(msg[2])
    8 #获取第12个元素
    ----> 9 print(msg[11])

    IndexError: string index out of range</pre>

    诶呀我去,怎么出了这么个玩意?一般大家看到这种错误,莫名的头疼,这时候我们会问:

    4.访问第一个元素索引为什么是0?

    序列的索引从0开始,所以我们访问第一个元素对应索引为0,那么我们访问第二个索引就是2-1,访问第N个元素,索引就是N-1;

    5.为什么会出错,这是什么情况?

    访问索引超过了序列长度就会报错,因为访问的元素不存在。

    6.怎么看这个错误?

    Python中出错后,会将出错行,出错信息提示出来,给了我们足够信息去解决问题,具体意思如下:

    image

    下次看到这种错误就查下序列长度与索引值。

    7.我想遍历整个序列,难道要msg[1],msg[2]...msg[n]么?

    如果你想,这种方式也是可以的;

    但是我们一般使用for循环,代码如下:

    <pre class="prettyprint hljs fsharp" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">print(msg)

    for 遍历序列,依次去msg中的每个元素,

    并赋值给val

    for val in msg:
    print(val)</pre>

    8.for循环为什么不会出错?

    for 循环内部做了异常处理,所以不会出错,我们直接使用就可以。

    9.切片操作是怎么回事?

    切片是Python中的操作符,类似函数操作,使用方式如下:

    sequence[start:stop]:start为起始索引,stop为结束索引,

    结果为:获取索引start到stop-1元素,返回新的序列;

    start默认为0,stop默认到最后;

    sequence[start:stop:step]:与上类似,step为步进值,默认为1;

    10.如何使用切片操作?

    举个例子,msg = 'hellowolrd',jupyter下操作如下:

    <pre class="prettyprint hljs bash" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">msg = 'helloworld'

    索引从0开始算

    获取索引2到4之间元素?

    print(msg[2:5]) #stop -1

    获取索引0到7之间元素?

    print(msg[0:8])

    获取索引5之后所有元素?

    print(msg[5:11]) #stop -1
    print(msg[5:]) #默认到最后
    print(msg[5:100])#会不会报错,为什么?</pre>

    输出结果:

    <pre class="hljs nginx" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 0.75em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">llo
    hellowor
    world
    world
    world</pre>

    11.切片操作有高级使用方式么?

    没有高不高级,看如何使用;

    同样一块面,你做成馒头他就2元,你做成蛋糕他就有一万种可能。

    理解知识点:序列有两种索引:正索引,负索引;

    先提出问题,然后自己尝试去实现,可以把答案写在品论区,再继续阅读:

    1>索引倒数第3个之后所有元素,

    2>索引从0到结束,隔一个取一个,

    3>索引倒数第二个开始,向前隔一个取一个元素,

    4>切片操作实现倒序,

    到这里,应该在自己环境下尝试练习了,我这边继续,具体实现如下:

    <pre class="prettyprint hljs bash" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">msg = 'helloworld'

    获取索引倒数第二个元素之后所有元素

    print(msg[-2:])

    获取索引从0到结束,隔一个取一个元素

    理解为,start为0,stop到最后,step为2的切片操作

    print(msg[::2])

    倒数第二个开始,向前隔一个取一个元素

    理解为:start为-2,stop到最前,step为-2的切片操作

    print(msg[-2::-2])

    切片操作实现倒序?

    print(msg[::-1])</pre>

    输出结果:

    <pre class="hljs nginx" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 0.75em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">ld
    hlool
    loolh
    dlrowolleh</pre>

    这些搞定了,我们就基本掌握切片操作了。

    12.序列支持运算符吗?

    运算符 是否支持
    算数运算符 支持加法与乘法操作,返回新的序列
    比较运算符 支持,返回True与False
    逻辑运算符 支持

    注意:必须是同种类型数据结构操作才有意义。

    13.我想要拼接字符串,如何操作?

    直接使用加法操作,如下:

    <pre class="hljs lua" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 0.75em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">wd1 = 'hello'
    wd2 = 'world'
    print(wd1 + wd2)
    print(wd1 * 2)</pre>

    输出结果:

    <pre class="hljs nginx" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 0.75em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">helloworld
    hellohello</pre>

    14.取序列长度?

    使用len方法:

    <pre class="hljs go" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 0.75em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">msg = 'helloworld'
    print(len(msg))</pre>

    输出结果:10

    15.能否通过索引遍历序列?

    能,使用range方法,生成索引,然后使用for循环遍历:

    <pre class="prettyprint hljs vim" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">msg = 'helloworld'

    获取长度,生成range对象

    使用for遍历range获取索引

    通过索引访问元素

    for index in range(len(msg)):
    print(msg[index])</pre>

    16.能否再遍历的时候获取索引与元素?

    需要使用enumerate:enumerate(iterable[, start])

    参数:iterable:可迭代对象,start:可选参数,指定起始位置;

    返回:索引与value对应的enumerate对象;

    可以使用for循环进行遍历,具体使用如下:

    <pre class="prettyprint hljs bash" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">msg = 'helloworld'

    创建enumerate对象

    items = enumerate(msg)

    遍历enumerate对象

    for item in items:
    print(item)</pre>

    输出结果:

    <pre class="prettyprint hljs scheme" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">(0, 'h')
    (1, 'e')
    (2, 'l')
    (3, 'l')
    (4, 'o')
    (5, 'w')
    (6, 'o')
    (7, 'r')
    (8, 'l')
    (9, 'd')</pre>

    17.还没真正获取索引,这个索引和元素在一起如何处理?

    是否还记得多元赋值:x,y = 1,2

    第一种方式:

    <pre class="prettyprint hljs xquery" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">msg = 'helloworld'
    for item in enumerate(msg):
    index, value = item
    print(index, value)</pre>

    第二种方式:

    <pre class="prettyprint hljs lua" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">msg = 'helloworld'
    for tindex, tvalue in enumerate(msg):
    print(tindex, tvalue)</pre>

    第一种容易理解,第二种怎么回事?

    for循环迭代的时候,首先从 enumerate取元素,而 enumerate的每个元素都是(index, values),我们这里加了两个变量tindex, tvalue去接受值,相当于多元赋值。

    18.判断元素是否存再序列中存在?

    使用操作符:in与not in

    in:元素是否在序列中,在返回True,否则返回False,例如:'a' in 'abc';

    not in:与in相反;

    实际操作如下:

    <pre class="prettyprint hljs python" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">#加法与乘法:
    msg = 'helloworld*'

    元素是否在序列中

    print('w in msg:', 'w' in msg)
    print('a in msg:', 'a' in msg)
    print('a not in msg:','a' not in msg)</pre>

    输出结果:

    <pre class="hljs vbscript" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 0.75em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">w in msg: True
    a in msg: False
    a not in msg: True</pre>

    好了,到这里我们对序列基本知识点就有了一定认识。

    相关文章

      网友评论

        本文标题:十八问,认识Python序列,解决疑难杂症!

        本文链接:https://www.haomeiwen.com/subject/ljdxxctx.html