zoukankan      html  css  js  c++  java
  • Python-2-序列及通用序列操作

    序列包括字符串,列表,元祖,序列中的每个元素都有编号,其中只有元祖不能修改
     
    通用序列操作包括索引、 切片、 相加、 相乘和成员资格检查
     
    索引
    >>> greeting = 'Hello'
    >>> greeting[0]
    'H'
    >>> greeting[-1]
    'o'
    >>> 'Hello'[1]
    'e'
    >>> fourth = input('Year: ')[3]
    Year: 2005
    >>> fourth
    '5'
     
    切片
    >>> tag = '<a href="http://www.python.org">Python web site</a>'
    >>> tag[9:30]
    >>> tag[32:-4]
    'Python web site'
    >>> numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    >>> numbers[3:6] [4, 5, 6]
    >>> numbers[0:1] [1]
    开始元素包括结尾元素不包括
    >>> numbers[-3:-1]
    [8, 9]
    >>> numbers[-3:0]
    【】
    >>> numbers[-3:]
    [8, 9, 10]
    >>> numbers[:3]
    [1, 2, 3]
    >>> numbers[:]
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    第三位为步长
    >>> numbers[0:10:1]
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    >>> numbers[0:10:2]
    [1, 3, 5, 7, 9]
    numbers[3:6:3]
    [4]
    >>> numbers[::4]
    [1, 5, 9]
    步长不能为0,否则无法向前移动,但可以为负数,即从右向左提取元素
    >>> numbers[8:3:-1]
    [9, 8, 7, 6, 5]
    >>> numbers[10:0:-2]
    [10, 8, 6, 4, 2]
    >>> numbers[0:10:-2]
    []
    >>> numbers[::-2]
    [10, 8, 6, 4, 2]
    >>> numbers[5::-2]
    [6, 4, 2]
    >>> numbers[:5:-2]
    [10, 8]
     
    相加
    >>> [1, 2, 3] + [4, 5, 6]
    [1, 2, 3, 4, 5, 6]
    >>> 'Hello,' + 'world!'
    'Hello, world!'
    >>> [1, 2, 3] + 'world!'
    Traceback (innermost last):
    File "<pyshell>", line 1, in ?
    [1, 2, 3] + 'world!'
    TypeError: can only concatenate list (not "string") to list
     
    乘法
    >>> sequence = [None] * 10
    >>> sequence
    [None, None, None, None, None, None, None, None, None, None]
    [],[0],[None]都表示空序列
     
    成员资格
    >>> permissions = 'rw'
    >>> 'w' in permissions
    True
    >>> 'x' in permissions
    False
    >>> users = ['mlh', 'foo', 'bar']
    >>> input('Enter your user name: ') in users
    Enter your user name: mlh
    True
    >>> subject = '$$$ Get rich now!!! $$$'
    >>> '$$$' in subject
    True
    长度,最小值和最大值
    >>> numbers = [100, 34, 678]
    >>> len(numbers)
    3
    >>> max(numbers)
    678
    >>> min(numbers)
    34
    >>> max(2, 3)
    3
  • 相关阅读:
    android图片特效处理之光晕效果
    Android 给图片加边框
    Android学习笔记进阶十三获得本地全部照片
    startActivityForResult()的用法
    Android学习笔记进阶十二之裁截图片
    Android学习笔记进阶十一图片动画播放(AnimationDrawable)
    Android控件开发之Gallery3D效果
    android中图片倒影、圆角效果重绘
    两种方法求最大公约数最小公倍数
    Nginx 负载均衡配置和策略
  • 原文地址:https://www.cnblogs.com/swefii/p/10735971.html
Copyright © 2011-2022 走看看