• python数据结构(一)------序列


      数据结构是通过某种方式(例如对元素进行编号)组织在一起的数据元素的集合;在Python中,最基本的数据结构是序列(sequence),序列中的每个元素被分配一个序列号--即元素的位置,也称为索引。

      python包含6种内建的序列,它们分别是列表、元组、字符串、Unicode字符串、buffer对象和xrange对象。

      本文将重点讲解python通用序列操作。


    1.1  索引

    1.2  分片

    1.3  序列相加

    1.4  乘法

    1.5  成员资格

    1.6  长度、最小值和最大值


    1.1  索引(index)

      序列中的所有元素都是有编号的----从0开始递增,可以通过编号分别访问。

    example1.1.1:

    #coding:utf-8
    
    #define varibles  months
    months = {
            'January',
            'February',
            'March',
            'April',
            'May',
            'June',
            'July',
            'August',
            'Septemer',
            'October',
            'November',
            'December'
    }
    
    #A list of number at the end of 1-31
    endings =['st','nd','rd']+17*['th']
                +['st','nd','rd'+7*['th']
                +['st']
    
    year = raw_input("Year:")
    month = raw_input('Month:')
    day = raw_input("Day[1-31]:')
    
    month_number = int(month)
    day_number = int(day)
    
    month_name = months[month_number-1]
    ordinal = day + endings[day_number -1]
    
    print month_name + ' ' + ordinal +', '+year
    View Code

    执行结果是:

    Year:1974
    Month:8
    Day(1-31):16
    August 16th, 1974
    View Code

    1.2  分片(slicing)

      分片操作通过冒号隔开的2个索引实现,第一个索引的元素是包含在分片内的,而第二个则不包含在分片内;

    参见代码:

    >>>numbers = [1,2,3,4,5]

    >>>numbers[1,3]

    [2,3]

    >>>numbers[-1:0]  #分片中最左边的索引比它右边的晚出现在序列中,结果就是一个空的序列

    []

    >>>numbers[-1:]  #

    [5]

    1.2.1  更大的步长(step length)

      普通的分片中,步长默认为1,通常隐式设置;

      步长为正时,第一个索引小于第二个索引;分片操作时从左往右提取元素,直至最后一个元素;

    >>>numbers[0:4:2]

    [1,3]

    >>>numbers[-4::2]

    [2,4]

      步长为负时,第一个索引大于第二个索引;分片操作时从右向左提取元素,直至最后一个元素;

    >>>numbers[4:2:-1]

    [5,4]

    >>>numbers[::-2]

    [5,3,1]

    1.3  序列相加(concatenation)

      通过“+”运算符可以进行序列的连接操作:

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

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

    >>>[1,2,3] + "character"

    Traceback(innermost last):

     file"<pyshell#2>"line 1.in?

      [1,2,3] + "character"

    TypeError:can only concatenate list(not "string") to lst

    即:2种相同的序列才能进行级联操作;

    1.4  乘法(multiplying)

      通过“*”运算符实现序列的重复操作;

    >>>"python"*3

    "pythonpythonpython"

    注:如果需要初始化一个10个元素的列表,可以使用None内建值;

    >>>sequence = [None]*10

    >>>sequence

    [None,None,None,None,None,None,None,None,None,None]

    example1.4.1:

    #coding:utf-8
    
    #以正确的宽度在居中的“盒子”内打印一个句子
    #注意,整数除法运算符(//)只能在python2.2以后的版本中使用,之前版本中只能使用普通除法
    sentence = raw_input("Sentence:")
    screen_width = 80
    test_width = len(sentence)
    box_width = text_width + 6
    left_margin = (screen_width - box_width)//2
    print ' '*left_margin  + '+' + '-'*left_margin +'-'*(box_width-2)+'-'*left_margin + "+"
    print ' '*left_margin + '| ' + " "*text_width + " |" 
    print ' '*left_margin + '| ' + sentence + '|'
    print ' '*left_margin + '| ' + " "*text_width + " |" 
    print ' '*left_margin  + '+' + '-'*left_margin +'-'*(box_width-2)+'-'*left_margin + "+"
    View Code

    执行结果是:

    sentence:He's a very naughty boy!

          +---------------------------------------------+

               |                |

               | He's a very naughty boy! |

               |                |

          +---------------------------------------------+

    1.5  成员资格(permission)

      通过“in”  运算符实现,返回值为布尔值;

    >>>"P" in "Python"

    True

    1.6  长度(len)、最小值(min)和最大值(max)

    >>>numbers = [100,34,768]

    >>>len(numbers)

    3

    >>>min(numbers)

    34

    >>>max(numbers)

    768

    PS:本人水平有限,有其他意见或建议可联系作者进行修改!

    路漫漫其修远兮,吾将上下而求索。。。

  • 相关阅读:
    [Bootstrap]全局样式(四)
    [Bootstrap]全局样式(三)
    [Bootstrap]全局样式(二)
    [Bootstrap]全局样式(一)
    [Bootstrap]概述
    原生JS-----一个剪刀石头布游戏
    聊聊 ES6 中的箭头函数
    jQuery中的 AJAX
    AJAX封装
    AJAX 初识
  • 原文地址:https://www.cnblogs.com/anthony-wsw/p/6431504.html
Copyright © 2020-2023  润新知