zoukankan      html  css  js  c++  java
  • 基础知识回顾——通用序列操作

    数据结构是计算机存储和组织数据的方式。Python中有三类四种内建的数据结构,分别是序列(List、Tuple)、映射(Dictionary)以及集合(Set)。

    所有序列类型都可以进行某些特定的操作,这些操作包括:索引、分片、加、乘、迭代以及检查某个元素是否属于序列的成员(成员资格),还有计算序列长度、找出最大元素和最小元素的内建元素。

    1.索引

    1 >>> greeting = ("hello")
    2 >>> greeting[1]
    3 'e'
    4 >>> greeting[-2]
    5 'l'

    2.分片

     1 >>> num = [1,5,4,7,0]  #从左往右分片起始索引为0,从右往左分片起始索引为-1
     2 >>> num[1:3]
     3 [5, 4]
     4 >>> num[0:-2]
     5 [1, 5, 4]
     6 >>> num[2:-2]
     7 [4]
     8 
     9 #开始点的元素包括在结果之中,结束点的元素则不在分片之内;
    10 #对于一个正数步长,会从序列头部开始向右提取元素,直到最后一个元素,对于负数步长,则从序列的尾部开始向左提取元素,直到第一个元素
    11 >>> num[0:4:2]            
    12 [1, 4]
    13 >>> num[::-2]
    14 [0, 4, 1]
    15 >>> num[2::-2]
    16 [4, 1]
    17 >>> num[:2:-2]
    18 [0]

    3.加法:相同类型的序列才能进行连接操作

     1 >>> [1,2,3]+[4,5]
     2 [1, 2, 3, 4, 5]
     3 >>> 'hello'+'world'
     4 'helloworld'
     5 >>> [1,2,3]+'hello'
     6 
     7 Traceback (most recent call last):
     8   File "<pyshell#201>", line 1, in <module>
     9     [1,2,3]+'hello'
    10 TypeError: can only concatenate list (not "str") to list

    4.乘法

    1 >>> 'python,'*5
    2 'python,python,python,python,python,'
    3 >>> seq = [None]*10    #初始化一个长度为10的列表
    4 >>> print seq
    5 [None, None, None, None, None, None, None, None, None, None]
    6 >>> print '!'*5 + 'Hello ,World' + '-'*10    
    7 !!!!!Hello ,World----------

    5.成员资格

     1 >>> 'P'in 'Python'
     2 True
     3 >>> 'p'in 'Python'
     4 False
     5 
     6 >>> 'P'in Python  #注意'Python'和Python区别
     7 
     8 Traceback (most recent call last):
     9   File "<pyshell#211>", line 1, in <module>
    10     'P'in Python
    11 NameError: name 'Python' is not defined

    6.迭代:通过for循环来遍历

     1 #并行迭代
     2 >>> names = ['ann','beth','tita','jane']
     3 >>> ages = ['21','19','18','22']
     4 >>> zip(names,ages)
     5 [('ann', '21'), ('beth', '19'), ('tita', '18'), ('jane', '22')]
     6 >>> for name,age in zip(names,ages):      #zip可以处理不等长的序列
     7     print name, 'is' ,age ,'years old'
     8 
     9 ann is 21 years old
    10 beth is 19 years old
    11 tita is 18 years old
    12 jane is 22 years old
    13 
    14 #索引迭代
    15 >>> strings = 'python'
    16 >>> for index,string in enumerate(strings):
    17     if '3' in strings:
    18         strings[index] = 'h'
    19 
    20 #翻转和排序迭代:reversed方法返回一个可迭代对象,sorted方法返回列表
    21 >>> sorted([2,0,7,5])
    22 [0, 2, 5, 7]
    23 >>> reversed('hello')
    24 <reversed object at 0x02FF8C70>
    25 >>> list(reversed('hello'))
    26 ['o', 'l', 'l', 'e', 'h']

    7.序列长度、最大值、最小值

    1 >>> num = [120,14,39]
    2 >>> len(num)
    3 3
    4 >>> max(num)
    5 120
    6 >>> min(num)
    7 14
  • 相关阅读:
    ipfs cluster 模式部署使用(docker-compose 环境运行)
    lerna import && add 使用&&常见问题解决
    memsql kafka集成
    memsql 6.7集群安装
    过程、性能基线、性能模型与目标之间的关系
    oracle extract函数
    ORACLE的VARCHAR2是字节还是字符
    infor系统日常问题解决笔记
    Oracle获取当前年、月、日的方法
    Windows下Redis中RedisQFork位置调整
  • 原文地址:https://www.cnblogs.com/Ryana/p/5968101.html
Copyright © 2011-2022 走看看