python 列表元组字典
Posted kezi
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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.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‘]
以上是关于python 列表元组字典的主要内容,如果未能解决你的问题,请参考以下文章