• python 变量、列表、元组、字典


    python 变量、列表、元组、字典

    1、python 变量赋值

    2、ptython  列表

    3、python 元组

    4、python  字典

     

     

    1、  Python变量赋值

        1.1变量的命名规则

        变量名只能是 字母、数字或下划线的任意组合

        变量名的第一个字符不能是数字

        以下关键字不能声明为变量名

       [ 'assert','and', 'as', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or',           'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']

     

         1.2变量赋值的三种方式

        

                 传统赋值:student = “kezi”

     

                 链式赋值: student= user = “kezi”

     

                 序列解包赋值: stuent,age = “kezi”,10

    >>> student="kezi"
    >>> print(student)
    kezi
    >>> student2=student
    >>> print(student2)
    kezi
    >>> student="keke"
    >>> print (student2)
    kezi
    >>> print(student)
    keke

    2、  python列表

     列表是我们最常用的数据类型之一,是一个元素以逗号分割,以中括号包围的,有序的,可修改、存储、的序列。

    例子:

    >>> student=["zhangshan","liuliu","taotao","junjun","xixi"]
    >>> student[1:4]
    ['liuliu', 'taotao', 'junjun']
    >>> student[2:-1]
    ['taotao', 'junjun']
    >>> student[2:-1]
    ['taotao', 'junjun']
    >>> student[2:]
    ['taotao', 'junjun', 'xixi']
    >>> student[0:2:]
    ['zhangshan', 'liuliu']
    >>> 

    2.2列表的方法

    列表的添加

    append

    追加,在列表的尾部加入指定的元素

    extend

    将指定序列的元素依次追加到列表的尾部

    insert

    将指定的元素插入到对应的索引位上,注意负索引

    列表的查找

    注 列表没有find方法

    count

    计数,返回要计数的元素在列表当中的个数

    index

    查找,从左往右返回查找到的第一个指定元素的索引,如果没有找到,报错

    列表的排序

    reverse

    索引顺序倒序

    sort

    按照ascii码表顺序进行排序

    列表的删除

     

     

    pop

    弹出,返回并删除指定索引位上的数据,默认-1

    remove

    从左往右删除一个指定的元素

    del

    删除是python内置功能,不是列表独有的


    例子:

    列表的添加

    >>> student
    ['zhangshan', 'liuliu', 'taotao', 'junjun', 'xixi']
    >>> student.insert(2,"honghonghong")   #默认是从下标是0开始的,0,1,2,3,4,,,,
    >>> student
    ['zhangshan', 'liuliu', 'honghonghong', 'taotao', 'junjun', 'xixi']
    >>> student ['zhangshan', 'liuliu', 'honghonghong', 'taotao', 'junjun', 'xixi'] >>> student.append("haihaihai") >>> student ['zhangshan', 'liuliu', 'honghonghong', 'taotao', 'junjun', 'xixi', 'haihaihai'] >>> student.extend("kaikaikai") >>> student ['zhangshan', 'liuliu', 'honghonghong', 'taotao', 'junjun', 'xixi', 'haihaihai', 'k', 'a', 'i', 'k', 'a', 'i', 'k', 'a', 'i']

    统计、查找

    >>> student
    ['zhangshan', 'liuliu', 'honghonghong', 'taotao', 'junjun', 'xixi', 'haihaihai', 'k', 'a', 'i', 'k', 'a', 'i', 'k', 'a', 'i']
    >>> student.count("k")
    3

      >>> student
       ['zhangshan', 'liuliu', 'honghonghong', 'taotao', 'junjun', 'xixi', 'haihaihai', 'a', 'k', 'a', 'i', 'k', 'a']
      >>> print (student.index("honghonghong"))
      2

    删除

    >>> student.remove("k")
    >>> student
    ['zhangshan', 'liuliu', 'honghonghong', 'taotao', 'junjun', 'xixi', 'haihaihai', 'a', 'i', 'k', 'a', 'i', 'k', 'a', 'i']
    >>> student.pop()
    'i'
    >>> student
    ['zhangshan', 'liuliu', 'honghonghong', 'taotao', 'junjun', 'xixi', 'haihaihai', 'a', 'i', 'k', 'a', 'i', 'k', 'a']
    >>> del student[8]
    >>> student
    ['zhangshan', 'liuliu', 'honghonghong', 'taotao', 'junjun', 'xixi', 'haihaihai', 'a', 'k', 'a', 'i', 'k', 'a']

     注:列表中可以建立子列表。

    浅copy

    import copy
    student=['zhangshan', 'liuliu', ['honghonghong', 'taotao'], 'junjun', 'xixi', 'haihaihai', 'a', 'k', 'a', 'i', 'k', 'a']
    student1=copy.copy(student)
    
    print (student)
    print(student1)
    student[2][0]="kaikaikai"
    print (student)
    print(student1)

    打印结果:

    ['zhangshan', 'liuliu', ['honghonghong', 'taotao'], 'junjun', 'xixi', 'haihaihai', 'a', 'k', 'a', 'i', 'k', 'a']
    ['zhangshan', 'liuliu', ['honghonghong', 'taotao'], 'junjun', 'xixi', 'haihaihai', 'a', 'k', 'a', 'i', 'k', 'a']
    ['zhangshan', 'liuliu', ['kaikaikai', 'taotao'], 'junjun', 'xixi', 'haihaihai', 'a', 'k', 'a', 'i', 'k', 'a']
    ['zhangshan', 'liuliu', ['kaikaikai', 'taotao'], 'junjun', 'xixi', 'haihaihai', 'a', 'k', 'a', 'i', 'k', 'a']

    为什么改  student ,student1也在变呢,由于student与student1的第二层内存地址是指向了同一个内存地址。

    深copy

    import copy
    student3=['zhangshan', 'liuliu', ['honghonghong', 'taotao'], 'junjun', 'xixi', 'haihaihai', 'a', 'k', 'a', 'i', 'k', 'a']
    student4=copy.deepcopy(student3)
    print (student3)
    print(student4)
    student3[2][0]="xinxinxin"
    print (student3)
    print(student4)
    student4[2][0]="yunyunyun"
    print (student3)
    print(student4)

    打印结果

    ['zhangshan', 'liuliu', ['honghonghong', 'taotao'], 'junjun', 'xixi', 'haihaihai', 'a', 'k', 'a', 'i', 'k', 'a']
    ['zhangshan', 'liuliu', ['honghonghong', 'taotao'], 'junjun', 'xixi', 'haihaihai', 'a', 'k', 'a', 'i', 'k', 'a']
    ['zhangshan', 'liuliu', ['xinxinxin', 'taotao'], 'junjun', 'xixi', 'haihaihai', 'a', 'k', 'a', 'i', 'k', 'a']
    ['zhangshan', 'liuliu', ['honghonghong', 'taotao'], 'junjun', 'xixi', 'haihaihai', 'a', 'k', 'a', 'i', 'k', 'a']
    ['zhangshan', 'liuliu', ['xinxinxin', 'taotao'], 'junjun', 'xixi', 'haihaihai', 'a', 'k', 'a', 'i', 'k', 'a']
    ['zhangshan', 'liuliu', ['yunyunyun', 'taotao'], 'junjun', 'xixi', 'haihaihai', 'a', 'k', 'a', 'i', 'k', 'a']

    深 copy 存放地址不在同一个地方了,因此打印结果不一样。

     3、python 元组

     元组是元素以逗号分割,以小括号包围的有序的,不可修改的序列。

    元组的特性:

    (1)元组可以不加括号

    (2)单元素元组需要加逗号

    (3)元组不可修改,所以我们在配置文件当中多看到元组

    元组的方法

    元组的查找

    index

    从左往右返回第一个遇到的指定元素的索引,如果没有,报错

    count

    返回元组当中指定元素的个数

        

    元组和字符串的区别

         (1)、  元组和字符串都是有序的,不可修改的序列

         (2)、  元组的元素可以是任何类型,字符串的元素只能是字符

         (3)、  元组的元素长度可以任意,字符串的元素长度只能为1

     4、python  字典

    字典一种key - value 的数据类型

    字典一个元素呈键值对的形式,以逗号分割,以大括号包围的无序的,可以修改的序列。

    字典是python基础数据类型当中唯一一个映射关系的数据类型

    字典的特点:

                因为字典是无序的,所以字典没有索引值,

                因为字典没有索引值,所以字典以键取值,(字典的键相当于列表的索引)

    字典的方法:

    字典的取值

    keys

    获取字典所有的键

    values

    获取字典所有的值

    get

    以键取值,如果指定键不存在,默认返回None,可以指定返回内容

    update

    更新指定键的内容,如果键不存在,创建

    setdefault

    设置默认,如果键存在,返回值,如果键不存在,创造键,值默认为None,值也可以自定义

    items

    返回字典键值呈元组形式的格式

    字典的删除

    pop

    弹出,返回并删除指定键对应的值

    popitem

    随机弹出一个键值元组,这里随机的原因是因为字典无序

    clear

    清空字典

    info={
    "s001":"zhangsan" ,
    "s002":"lisi",
    "s003":"wangwu"
    }
    print(info)

    打印结果
    {'s003': 'wangwu', 's001': 'zhangsan', 's002': 'lisi'}

    增加

    info['s004']="wangmazi"
    print(info)

    打印结果
    {'s004': 'wangmazi', 's002': 'lisi', 's001': 'zhangsan', 's003': 'wangwu'}

    修改

    info['s001']="huangda"
    print(info)

    打印结果

    {'s003': 'wangwu', 's004': 'wangmazi', 's001': 'huangda', 's002': 'lisi'}

    删除

    info.pop("s004")
    print(info)

    打印结果
    {'s001': 'huangda', 's002': 'lisi', 's003': 'wangwu'}

    del info["s001"]
    print(info)

    打印结果

    {'s002': 'lisi', 's003': 'wangwu'}

    随机删除

    info.popitem()  #随机删除
    print(info)

    打印结果
    {'s003': 'wangwu'}

    查找

    info={
    "s001":"zhangsan" ,
    "s002":"lisi",
    "s003":"wangwu"
    }
    print(info)
    
    
    print (info['s002'])
    print(info["s004"])没有查到,要报错
    print(info.get('s001'))如果没有查到,也不会报错
    print("s003" in info) 标准用法


    打印结果
    lisi
    print(info["s004"])
    KeyError: 's004'   没有找到,报错

    zhangsan
    True

    多级字典嵌套及操作

    ddress1={"四川":["成都",'锦阳']
             ,"广东":["广州","佛山"]
             ,"湖南":["长沙","益阳"]
    
    
    }
    print(address1["四川"][0]) 查找

    address1["四川"][1]="德阳" #修改
    print(address1)

    打印结果
    成都
    {'四川': ['成都', '德阳'], '广东': ['广州', '佛山'], '湖南': ['长沙', '益阳']}

    print(address1.values())
    print(address1.keys())
    address1.setdefault("直辖市","上海")
    print(address1)
    
    #update
    qita={"特区":"香港","福建":"厦门"}
    address1.update(qita)
    print(address1)
    打印结果
    dict_values([['成都', '德阳'], ['广州', '佛山'], ['长沙', '益阳']])
    dict_keys(['四川', '广东', '湖南'])
    {'四川': ['成都', '德阳'], '广东': ['广州', '佛山'], '湖南': ['长沙', '益阳'], '直辖市': '上海'}
    {'直辖市': '上海', '特区': '香港', '四川': ['成都', '德阳'], '广东': ['广州', '佛山'], '湖南': ['长沙', '益阳'], '福建': '厦门'}
    
    
    print(address1.items())#转化为列表
    打印结果
    dict_items([('福建', '厦门'), ('特区', '香港'), ('直辖市', '上海'), ('广东', ['广州', '佛山']), ('四川', ['成都', '德阳']), ('湖南', ['长沙', '益阳'])])
    
    
    
    bb=address1.fromkeys([6,7,9],"uuuu")
    print(bb)
    
    cc=dict.fromkeys([1,2,3],[1,{"name":"kezi"},555])全部都修改了
    print(cc)
    
    打印结果
    {9: 'uuuu', 6: 'uuuu', 7: 'uuuu'}
    {1: [1, {'name': 'kezi'}, 555], 2: [1, {'name': 'kezi'}, 555], 3: [1, {'name': 'kezi'}, 555]}
    
    循环
    for i in address1:
        print(i,address1[i])
    打印结果
    广东 ['广州', '佛山']
    特区 香港
    四川 ['成都', '德阳']
    直辖市 上海
    福建 厦门
    湖南 ['长沙', '益阳']
    
    
    for i in address1:
        print(i)
    
    打印结果
    
    广东
    特区
    四川
    直辖市
    福建
    湖南

    数据类型的总结

          

     

    str

    list

    tuple

    dict

    是否有序

    是否可修改

    方法多少

    很多

    一般

    很少

    较多   映射关系

     

  • 相关阅读:
    OSG节点访问和遍历
    osg ifc数据渲染着色器
    osg qt kdchart 开发施工过程模拟软件
    KDChart example
    Qt kdChart 甘特图案例
    Qt KDChart编译
    osg 3ds模型加载与操作
    osg 三维模型加载与解析(fbx、3ds、ive、obj、osg)
    osg fbx 模型结构操作
    osg fbx 模型树结构
  • 原文地址:https://www.cnblogs.com/kezi/p/11804185.html
Copyright © 2020-2023  润新知