列表,元祖,字典小记

发布时间:2019-03-06 17:26:22编辑:auto阅读(2068)

    一、列表功能及方法

    1,建立列表,列表的元素可以包含任何数数据类型,可以是数字,字符串,列表,元组,字典,集合和布尔值

    li = [1, 12, 9, "age", ["石振文", ["19", 10], "庞麦郎"], "alex", True]  # 通过list类创建的对象,li
    View Code

    列表list在Python中其实就是一个类,赋值后就是调用这个类的一个对象,list.xx()就是对象的方法,也可称之为参数。

    2,列表的在后面添加元素,方法为append()

    1 li = [11, 22, 33, 22, 44]
    2  li.append(5)
    3  li.append("alex")
    4  li.append([1234,2323])
    5  print(li)
    View Code

     2 清空列表
    li.clear()
    print(li)

    3 拷贝,浅拷贝
    v = li.copy()
     print(v)


     4. 计算元素出现的次数
     v = li.count(22)
     print(v)

    5. 扩展原列表,参数:可迭代对象,方法为extend()会把一个可迭代的对象,迭代出来作为元素加入到列表中,也就是替我们用For循环逐个加入到列表。

    li = [11, 22, 33, 22, 44]
    li.append([9898,"不得了"])
    print(li)
    View Code

    输出结果为:

    [11, 22, 33, 22, 44, [77, 99]]
    >>> li = [11, 22, 33, 22, 44]
    >>> li.index(22)
    1        只查到第一个元素22所在的索引位置
    View Code

     

    [11, 22, 33, 22, 44, 9898, '不得了']

    >>> li = [11, 22, 33, 22, 44]
    >>>
    >>> li.extend(234) #如果输入的对象是不可迭代的,它会报错。
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: 'int' object is not iterable
    View Code

    它与append的差别在于,append会把整个输入对象作为一个元素加入到列表的后面。

     

    6. 根据值获取当前值索引位置(左边优先)方法为index(xx)

    >>> li = [11, 22, 33, 22, 44]
    >>> li.append([77,99])
    >>> print(li)
    [11, 22, 33, 22, 44, [77, 99]]
    View Code

    7. 在指定索引位置插入元素 ,方法为insert()

    >>> li = [11, 22, 33, 22, 44]
    >>> li.insert(0,99)  #指定在第一个位置插入99
    >>> print(li)
    [99, 11, 22, 33, 22, 44]
    >>> li.insert(2,99)  #指定在第三个位置插入99
    >>> print(li)
    [99, 11, 99, 22, 33, 22, 44]
    View Code

     8、 删除某个值(1.指定索引;2. 默认最后一个),并获取删除的值
    li = [11, 22, 33, 22, 44]
    v = li.pop()
    print(li)

    [11, 22, 33, 22]

    print(v)

    44

     li = [11, 22, 33, 22, 44]
    v = li.pop(1)
     print(li)

     [22, 33, 22, 44]
     print(v)

    11

    9. 删除列表中的指定值,左边优先  方法remove()
    li = [11, 22, 33, 22, 44]
    li.remove(22)
    print(li)

    [11,  33, 22, 44]     只删除第一个元素22
    PS: pop 默认从最后一个删除, remove默认左边优先,  del li[0] 删除指定索引位置的元素   del li[7:9] 切片整段删除,  clear清空列表。

     

    10、 索引取值

    li = [11, 22, 33, 22, 44]

    print(li[3])

    33

    11,切片,切片结果也是列表

    li = [11, 22, 33, 22, 44]
    print(li[2:4])   取值的索引范围 大于等2,小于4.

    [33,22]

     

    二、元组功能及方法

    元组,元素不可被修改,不能被增加或者删除
    tuple
    tu = (11,22,33,44)
    tu.count(22),获取指定元素在元组中出现的次数
    tu.index(22)

    三、字典的功能和方法

    # 字典
    # dict
    # dict
    # dic = {
    # "k1": 'v1',
    # "k2": 'v2'
    # }
    # 1 根据序列,创建字典,并指定统一的值
    # v = dict.fromkeys(["k1",123,"999"],123)
    # print(v)

    # 2 根据Key获取值,key不存在时,可以指定默认值(None)
    # v = dic['k11111']
    # print(v)
    # v = dic.get('k1',111111)
    # print(v)

    # 3 删除并获取值
    # dic = {
    # "k1": 'v1',
    # "k2": 'v2'
    # }
    # v = dic.pop('k1',90)
    # print(dic,v)
    # k,v = dic.popitem()
    # print(dic,k,v)

    # 4 设置值,
    # 已存在,不设置,获取当前key对应的值
    # 不存在,设置,获取当前key对应的值
    # dic = {
    # "k1": 'v1',
    # "k2": 'v2'
    # }
    # v = dic.setdefault('k1111','123')
    # print(dic,v)

    # 5 更新
    # dic = {
    # "k1": 'v1',
    # "k2": 'v2'
    # }
    # dic.update({'k1': '111111','k3': 123})
    # print(dic)
    # dic.update(k1=123,k3=345,k5="asdf")
    # print(dic)

    # 6 keys() 7 values() 8 items() get update
    ##########

     

    # 1、基本机构
    # info = {
    # "k1": "v1", # 键值对
    # "k2": "v2"
    # }
    #### 2 字典的value可以是任何值
    # info = {
    # "k1": 18,
    # "k2": True,
    # "k3": [
    # 11,
    # [],
    # (),
    # 22,
    # 33,
    # {
    # 'kk1': 'vv1',
    # 'kk2': 'vv2',
    # 'kk3': (11,22),
    # }
    # ],
    # "k4": (11,22,33,44)
    # }
    # print(info)

    #### 3 布尔值(1,0)、列表、字典不能作为字典的key
    # info ={
    # 1: 'asdf',
    # "k1": 'asdf',
    # True: "123",
    # # [11,22]: 123
    # (11,22): 123,
    # # {'k1':'v1'}: 123
    #
    # }
    # print(info)

    # 4 字典无序

    # info = {
    # "k1": 18,
    # "k2": True,
    # "k3": [
    # 11,
    # [],
    # (),
    # 22,
    # 33,
    # {
    # 'kk1': 'vv1',
    # 'kk2': 'vv2',
    # 'kk3': (11,22),
    # }
    # ],
    # "k4": (11,22,33,44)
    # }
    # print(info)

    # 5、索引方式找到指定元素
    # info = {
    # "k1": 18,
    # 2: True,
    # "k3": [
    # 11,
    # [],
    # (),
    # 22,
    # 33,
    # {
    # 'kk1': 'vv1',
    # 'kk2': 'vv2',
    # 'kk3': (11,22),
    # }
    # ],
    # "k4": (11,22,33,44)
    # }
    # # v = info['k1']
    # # print(v)
    # # v = info[2]
    # # print(v)
    # v = info['k3'][5]['kk3'][0]
    # print(v)

    # 6 字典支持 del 删除
    # info = {
    # "k1": 18,
    # 2: True,
    # "k3": [
    # 11,
    # [],
    # (),
    # 22,
    # 33,
    # {
    # 'kk1': 'vv1',
    # 'kk2': 'vv2',
    # 'kk3': (11,22),
    # }
    # ],
    # "k4": (11,22,33,44)
    # }
    # del info['k1']
    #
    # del info['k3'][5]['kk1']
    # print(info)

    # 7 for循环
    # dict
    # info = {
    # "k1": 18,
    # 2: True,
    # "k3": [
    # 11,
    # [],
    # (),
    # 22,
    # 33,
    # {
    # 'kk1': 'vv1',
    # 'kk2': 'vv2',
    # 'kk3': (11,22),
    # }
    # ],
    # "k4": (11,22,33,44)
    # }
    # for item in info:
    # print(item)
    #
    # for item in info.keys():
    # print(item)

    # for item in info.values():
    # print(item)

    # for item in info.keys():
    # print(item,info[item])

    # for k,v in info.items():
    # print(k,v)

    # True 1 False 0
    # info ={
    # "k1": 'asdf',
    # True: "123",
    # # [11,22]: 123
    # (11,22): 123,
    # # {'k1':' v1'}: 123
    #
    # }
    # print(info)

关键字