zoukankan      html  css  js  c++  java
  • 自兴人工智能——通用序列操作

    序列是Python中最基本的数据结构,序列中的每个元素都分配一个数字,代表它在序列中的位置

    序列中所有的元素都是有编号的从开始递增

    字符串是由字符组成的序列:

    str="hello"

    print  str[0]

    >>>"h"

    print  str[1]

    >>>"e"

    上面的是从左往右开始编号的,在Python中,序列也可以从右往左通过编号获取元素,最右边的元素索引值为-1,从右往左依次递减

    print  str[-1]

    >>>"o"

    print str[-2]

    >>>"l"

    分片:索引用来对单个元素进行访问,使用分片可以对一定范围内的元素进行访问,分片通过冒号相隔的两个索引实现

     number=[1,2,3,4,5,6,7,8,9]

    print  number[1:3]

    >>> [2,3]#取前不取后

    print  number[6:]

    >>>[7,8,9]

    序列相加:

    使用加号可以进行序列的连接操作

    print  [1,2,3]+[4,5,6]

    >>>[1,2,3,4,5,6]

    s=" hello,"

    h=" wold"

    >>>" hello,wold"

    乘法:用一个数字x乘以一个序列会生成新的序列中的乘法

    print  " hello"*5

    >>>" hellohellohellohellohello"

    好了,今天的介绍就到这里了,希望能帮到你们

  • 相关阅读:
    UVA 1386 Cellular Automaton
    ZOJ 3331 Process the Tasks
    CodeForces 650B Image Preview
    CodeForces 650A Watchmen
    CodeForces 651B Beautiful Paintings
    CodeForces 651A Joysticks
    HUST 1601 Shepherd
    HUST 1602 Substring
    HUST 1600 Lucky Numbers
    POJ 3991 Seinfeld
  • 原文地址:https://www.cnblogs.com/zhongsiyi/p/8456465.html
Copyright © 2011-2022 走看看