zoukankan      html  css  js  c++  java
  • Python 数据类型-2

    序列

    包括:字符串 列表 元组

    索引操作和切片操作

    索引操作:可以从序列中抓取一个特定的项目

    切片操作: 获取序列的一个切片,即一部分序列

    序列的通用方法:

    len() 求序列的长度

    + 连接2个序列

    * 重复序列元素

    in 判断元素是否在序列中

    max() 返回最大值

    min() 返回最小值

    cmp(x,y) 比较两个序列是否相等 返回0 相等 正数 X > Y, 负数 x < y

    l = (1, 2, 4, "ad", "b")
    a = (4, 6, 8, "gh", "Li")
    print(len(l))
    print("a" in l)
    print(l + a)
    print(max(a))
    print(min(l))
    print(cmp(a, l))
    
    执行:
    C:Python27python.exe D:/Python/type-of-data1.py
    5
    False
    (1, 2, 4, 'ad', 'b', 4, 6, 8, 'gh', 'Li')
    gh
    1
    1
    
    Process finished with exit code 0
    
    

    元组(tuple)

    >和列表十分相似
    
    >和字符串一样是不可变的
    
    >可以存储一系列的值
    
    >通常用在用户定义的函数能够安全的采用一组值的时候,即被使用的元组的值是不会改变的
        用于接收函数的返回值
        
    

    定义元组(tuple)

    t = (1,) 定义只有一个元素的元组,当元素个数为1个时,后需加 ','
        In [17]: t = (1,)
        In [18]: type(t)
        Out[18]: tuple
    
        In [19]: t = (1)
        In [20]: type(t)
        Out[20]: int
    
        In [21]: t = ('1')
        In [22]: type(t)
        Out[22]: str
    
    t = ('a',1,(1,))  元组里可存放元组
    
    

    操作:

    变量接收元组的值(元组的拆分)
        In [23]: t = ('a','b','c')
    
        In [24]: first, second, third = t
    
        In [25]: first
        Out[25]: 'a'
    
        In [26]: second
        Out[26]: 'b'
    
        In [27]: third
        Out[27]: 'c'
    #######################################
    t = ('a', 'b', 'c', 'b')
    
    first, second, third, therd = t
    
    print(first)
    print(second)
    print(third)
    print(therd)
    
    执行:
    C:Python27python.exe D:/Python/type-of-data1.py
    a
    b
    c
    b
    
    Process finished with exit code 0
    
    

    方法:

    t.count()
    >count(...)

    >T.count(value) -> integer -- return number of occurrences of value
    
    >返回值出现的次数
    
        In [30]: t = ('a','b','c')
    
        In [31]: t.count('b')
        Out[31]: 1
    
        In [32]: t.count('c')
        Out[32]: 1
        
        In [33]: t.count('a')
        Out[33]: 1
    
        In [34]: t.count('bc')
        Out[34]: 0
    ############################################################################
    t = ("a", "b", "b", "c", 1)
    # t.count()
    print(t.count('b'))
    print(t.count('a'))
    print(t.count(1))
    print(t.count('t '))
    
    执行:
    C:Python27python.exe D:/Python/type-of-data1.py
    2
    1
    1
    0
    
    Process finished with exit code 0
    
    

    t.index()
    >index(...)

    T.index(value, [start, [stop]]) -> integer -- return first index of value.

    Raises ValueError if the value is not present.

    返回一个值的索引,有多个会显示第一个

        In [36]: t.index('b')
        Out[36]: 1
    
        In [37]: t.index('a')
        Out[37]: 0
    
        In [38]: t.index('c')
        Out[38]: 2
    
        In [39]: t.index('abc')
        ---------------------------------------------------------------------------
        ValueError                                Traceback (most recent call last)
        <ipython-input-39-686534983c5a> in <module>()
        ----> 1 t.index('abc')
    
        ValueError: tuple.index(x): x not in list
    #######################################################################################
    t = ("a", "b", "b", "c", 1)
    # t.index()
    print(t.index("b"))
    print(t.index("a"))
    print(t.index("c"))
    print(t.index(1))
    
    执行:
    C:Python27python.exe D:/Python/type-of-data1.py
    1
    0
    3
    4
    
    Process finished with exit code 0
    
    

    列表(list)

    处理一组有序项目的数据结构,即可以列表中存储一个序列的项目

    是可变类型的数据

    定义列表:
    

    list1 = [] 创建空列表
    list2 = list() 通过list() 函数创建列表
    list3 = ['a',1,('b','c')]
    list4 = ['a',1,('b','c'),['a',1,('b','c')]]

    list1 = []
    list2 = list()
    list3 = ["a", 1, ("b", "c")]
    list4 = ["a", 1, {"b", "c"}, ["a", 1, ("b", "c") ]]
    
    print(list1)
    print(type(list1))
    print(list2)
    print(type(list2))
    print(list3)
    print(type(list3))
    print(list4)
    print(type(list4))
    
    执行:
    C:Python27python.exe D:/Python/type-of-data1.py
    []
    <type 'list'>
    []
    <type 'list'>
    ['a', 1, ('b', 'c')]
    <type 'list'>
    ['a', 1, set(['c', 'b']), ['a', 1, ('b', 'c')]]
    <type 'list'>
    
    Process finished with exit code 0
    

    方法:

    list.append()

    append(...)
    L.append(object) -- append object to end
    追加一个元素到列表最后

    list1 = ["a", "b", "c", 100, "abcd"]
    
    list1.append("helloe")
    
    print(list1)
    
    执行:
    C:Python27python.exe D:/Python/type-of-data1.py
    ['a', 'b', 'c', 100, 'abcd', 'helloe']
    
    Process finished with exit code 0
    
    

    list.index() 查找并返回元素的下标

    list1 = [54, 69, 100, 10, 55]
    print(list1.index(10))
    
    执行:
    C:Python27python.exe D:/Python/type-of-data1.py
    3
    
    Process finished with exit code 0
    

    list.extend()
    >extend(...)
    >L.extend(iterable) -- extend list by appending elements from the iterable
    > 追加一个可迭代的对象到列表中(通过从iterable附加元素来扩展列表)

    list1 = [54, 69, 100, 10, 55]
    list1.extend(range(1,10))
    print(list1)
    
    执行:
    C:Python27python.exe D:/Python/type-of-data1.py
    3
    [54, 69, 100, 10, 55, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    
    Process finished with exit code 0
    
    

    list.insert()
    >insert(...)
    >L.insert(index, object) -- insert object before index
    > 在某个下标前插入元素

    list1 = ["a", "b", "c", 100, "abcd"]
    
    list1.insert(1, "world")
    print(list1)
    
    执行:
    C:Python27python.exe D:/Python/type-of-data1.py
    ['a', 'world', 'b', 'c', 100, 'abcd']
    
    Process finished with exit code 0
    

    list.remvoe() 删除某个元素

    list1 = [54, 69, 100, 10, 55]
    list1.remove(100)
    print(list1)
    
    执行:
    C:Python27python.exe D:/Python/type-of-data1.py
    [54, 69, 10, 55]
    
    Process finished with exit code 0
    
    

    list.sort()
    >sort(...)
    >L.sort(cmp=None, key=None, reverse=False) -- stable sort IN PLACE;
    >cmp(x, y) -> -1, 0, 1
    > 给列表排序

    list1 = [54, 69, 100, 10, 55]
    list1.sort()
    print(list1)
    
    执行:
    C:Python27python.exe D:/Python/type-of-data1.py
    [10, 54, 55, 69, 100]
    
    Process finished with exit code 0
    
    

    list.reverse() 倒序

    list1 = [54, 69, 100, 10, 55]
    list1.reverse()
    print(list1)
    
    执行:
    C:Python27python.exe D:/Python/type-of-data1.py
    [55, 10, 100, 69, 54]
    
    Process finished with exit code 0
    

    list.pop()
    >pop(...)
    >L.pop([index]) -> item -- remove and return item at index (default last).
    >Raises IndexError if list is empty or index is out of range.
    > 删除并返回某个下标的元素(不指定下标默认为最后一个)

    list1 = [54, 69, 100, 10, 55]
    print(list1.pop())
    print(list1)
    print(list1.pop(2))
    print(list1)
    
    执行:
    C:Python27python.exe D:/Python/type-of-data1.py
    55
    [54, 69, 100, 10]
    100
    [54, 69, 10]
    
    Process finished with exit code 0
    

    删除
    del list[n] 删除List中的某个元素
    del list 删除一个列表
    del a 删除变量a
    del tuple 删除一个元组

    list.remove()

    list[] = x 修改某个元素

    var in list 查找某个元素是否在列表中

    切片

    In [9]: a = 'abcdef'
    
    In [10]: a[0:2]
    Out[10]: 'ab'
         取下标0到2 下标为2的不显示
    In [11]: a[:2]
    Out[11]: 'ab'
        取下标0到2 下标为2的不显示
    In [12]: a[:-1]
    Out[12]: 'abcde'
        取下标0到倒数第1个 下标为倒数第1个的不显示 
    In [13]: a[::2]
    Out[13]: 'ace'
        步长为2,即取下标为0、2、4、6
    In [14]: a[-3:-1]
    Out[14]: 'de'
        从倒数第3个取到倒数第1个,倒数第1个不显示
    In [15]: a[-1:-4:-1]
    Out[15]: 'fed'
        反向切片,从倒数第一个取到倒数第4个,倒数第4个不显示
    In [16]: a[-1:-4:-2]
    Out[16]: 'fd'
        反向切片,从倒数第一个取到倒数第4个,步长为2
    

    总结:

    a[A:B:C]
    
    A:切片开始的下标,包含
    B:切片结束的下标,不包含
    C:正数时表示步长
      负数时表示进行反向切片及步长
    
  • 相关阅读:
    Windows下memcache安装使用
    Linux 下memcache安装及使用
    C语言第五节scanf函数
    C语言第四节数据类型、常量、变量
    C语言第三节关键字、标识符、注释
    C语言第一节 C语言程序与开发工具
    快到而立之年了,可是能撑得起而立吗?
    idea-安装SequenceDiagram插件-生成时序图
    mysql中 查询一对多关系的时候,获取最新的一条
    判断多个时间段区间是否有重叠
  • 原文地址:https://www.cnblogs.com/lijunjiang2015/p/7719920.html
Copyright © 2011-2022 走看看