列表、元组、字典三种高级变量

自学整理记录,大神见笑

列表

  • 列表是Python中使用最频繁的数据类型,类似Java的数组
  • 列表用[]定义,数据之间用,分隔
  • 列表的索引从0开始
  • 索引就是列表中的位置编号,也叫下标

列表的方法

1.增

1.1 在索引处插入数据→insert

  • 列表.insert(索引,数据)

list_one = [1, 2, 3]
print list_one
list_one.insert(1, 9)
print list_one

  • 在列表list_one索引1处插入数字9

  • 输出如下:


    insert1.jpg
  • 注:如果索引大于原列表长度,则会追加到原列表上,如下

list_one = [1, 2, 3]
print list_one
list_one.insert(10, 9)
print list_one

  • 输出如下:
insert2.png

1.2 在末尾追加数据→append

  • 列表.append(数据)

list_one = [1, 2, 3]
print list_one
list_one.append(9)
print list_one

  • 将9追加到list_one列表末尾
  • 输出如下:
append.png

1.3 将列表2的数据追加到列表1→extend

  • 列表1.extend(列表2)

list_one = [1, 2, 3]
list_two = [1, 8, 9]
print list_one
list_one.extend(list_two)
print list_one

  • 将list_two的列表元素追加到list_one中
  • 输出如下:
extend.png

2.删

2.1 删除指定索引的数据→del

  • del 列表[索引]

list_one = [1, 2, 3]
print list_one
del list_one[1]
print list_one

  • 将列表list_one索引1的位置的元素删除
  • 输出如下:
del.png
  • 这里要注意的是:del方法里的参数索引值需要在列表长度范围内,否则会报错,如下

list_one = [1, 2, 3]
print list_one
del list_one[4]
print list_one

  • list_one长度为3,最大索引值2,而函数del的参数索引值为4,会报错
  • 输出如下:
del_error.png

2.2 删除第一次出现的指定数据→remove

  • 列表.remove(数据)

list_one = [1, 2, 3]
print list_one
list_one.remove(2)
print list_one

  • 将列表list_one中的元素2删除
  • 输出如下:
remove.png
  • 这里要注意的是:remove方法的参数数据必须是列表中存在的,否则会报错,如下

list_one = [1, 2, 3]
print list_one
list_one.remove(6)
print list_one

  • 输出如下:
remove_error.png

2.3 删除指定索引的数据→pop

  • 列表.pop[索引]

list_one = [1, 2, 3]
print list_one
list_one.pop(1)
print list_one

  • 将列表list_one中索引为1位置的元素删除
  • 输出如下:
pop1.png
  • 注:如果没有在pop方法中填入参数,则会默认删除列表最后一位元素,如下

list_one = [1, 2, 3]
print list_one
list_one.pop()
print list_one

  • 删除列表list_one最后一位元素
  • 输出如下
pop2.png
  • 注:如果pop方法中的参数索引值不能大于列表的长度,否则则会报错,如下

list_one = [1, 2, 3]
print list_one
list_one.pop(4)
print list_one

  • 列表list_one最大长度为3,最大索引为2,但pop方法中的参数为4,删除索引为4的位置
  • 输出如下
pop3.png

2.4 清空列表→clear

  • 列表.clear

list_one = [1, 2, 3]
print(list_one)
list_one.clear()
print(list_one)

  • 清空列表list_one中的元素
  • 输出如下:
clear1.jpg
  • 这里要注意的是:clear该方法只能在python3以上使用,python2会报错,输出如下
clear2.png

3.改

  • 列表[索引] = 数据

list_one = [1, 2, 3]
print(list_one)
list_one[1] = 7
print(list_one)

  • 修改列表list_one索引值为1的元素为7
  • 输出如下:
改.png

4.查

4.1 列表[索引]

  • 列表[索引]

list_one = [1, 2, 3]
print(list_one)
a = list_one[1]
print(a)

  • 查询列表list_one中索引值为1的元素
  • 输出如下:
查1.png
  • 注:索引值不能大于列表的长度,否则会报错,如下

list_one = [1, 2, 3]
print(list_one)
a = list_one[4]
print(a)

  • 输出如下:
查2.png

4.2 查找该数据第一次出现的索引→index

  • 列表.index(数据, 开始查找的位置, 结束查找的位置)

list_one = [1, 2, 3, 2, 4, 5, 2, 6]
print(list_one)
a = list_one.index(2)
print(a)

  • 查询列表list_one中第一次出现元素2的位置
  • 输出如下:
index1.png
  • 注:可以在index方法中填入查询范围,在索引a到索引b之间查询,如下

list_one = [1, 2, 3, 2, 4, 5, 2, 6]
print(list_one)
a = list_one.index(2, 2, 4)
print(a)

  • 在列表list_one中在索引2到索引4之间(左闭右开)查询元素2的位置
  • 输出如下:
index2.png
  • 注:如果数据在列表中查询不到,则会报错,如下

list_one = [1, 2, 3, 2, 4, 5, 2, 6]
print(list_one)
a = list_one.index(2, 0, 1)
print(a)

  • 在列表list_one索引0到1之间(左闭右开)查询数据2的位置
  • 输出如下:
index3.png

5.统计

5.1 列表长度→len

  • len(列表)

list_one = [1, 2, 3, 2, 4, 5, 2, 6]
print(list_one)
a = len(list_one)
print(a)

  • 查询列表list_one的长度
  • 输出如下:
len.png

5.2 数据在列表中出现的次数→count

  • 列表.count(数据)

list_one = [1, 2, 3, 2, 4, 5, 2, 6]
print(list_one)
a = list_one.count(2)
print(a)

  • 查询列表list_one中元素2出现的次数
  • 输出如下:
count1.png
  • 注:如果列表中不存在count查询的元素,则返回0,如下

list_one = [1, 2, 3, 2, 4, 5, 2, 6]
print(list_one)
a = list_one.count(7)
print(a)

  • 查询列表list_one中数据7出现的次数
  • 输出如下:
count2.png

6.排序

6.1 排序→sort

  • 列表.sort()

list_one = [1, 2, 3, 2, 4, 5, 2, 6]
print(list_one)
list_one.sort()
print(list_one)

  • 将列表list_one正序排序
  • 输出如下:
sort1.png
  • 注:当然字母也可以排序,同时可以混合排序,排序规则为数字在前,然后是字母大写,然后是字母小写,如下

list_one = ["c", "d", "b", "a", "t", "z", "r", "d", "C", 6]
print(list_one)
list_one.sort()
print(list_one)

  • 将列表list_one正序排序
  • 输出如下:
sort2.png
  • 注:如果需要将列表倒序排序,只需要在sort方法中加一参数reverse=True即可,如下

list_one = ["c", "d", "b", "a", "t", "z", "r", "d", "C", 6]
print(list_one)
list_one.sort(reverse=True)
print(list_one)

  • 将列表list_one倒序排序
  • 输出如下:
sort3.png

6.2 反转→reverse

  • 列表.reverse()

list_one = ["c", "d", "B", 6, "a", "T", "z", "r", "d", "C"]
print(list_one)
list_one.reverse()
print(list_one)

  • 将列表list_one反转
  • 输入如下:
sort4.png

关键字、函数和方法

  • 关键字是Python内置,不需要小括号
  • 函数封装独立功能,可直接调用
  • 方法封装独立功能,由对象调用,表示针对这个对象要做的操作

循环遍历

  • 使用for实现迭代遍历

# for 循环内部使用的变量 in 列表
for name in name_list:
 循环内部针对列表元素进行操作
 print(name)

列表应用场景

  • 列表可以存放不同类型的数据,但实际应用一般都是存储相同类型的数据

元组

元组定义

  • 元组的元素不能修改
  • 元组用()定义
  • 元组也用分隔
  • 元组也是从0开始
  • 元组是tuple

定义元组

  • 空元组

empty_tuple = ()

  • 只包含一个元素的元组,在这个元素后加,号,否则系统会认为是int类型

single_tuple = (5,)

  • 定义正常元组

tuple = ("zhangsan", 18, 1.75)

元组的方法

1.查

1.1 获取索引位置的数据→元组[索引]

  • 元组[索引]

tuple_one = (1, 2, 3)
print(tuple_one)
a = tuple_one[0]
print(a)

  • 查询元组tuple_one中索引为0的元素
  • 输出如下:
tuple1.jpg

1.2 查找该数据第一次出现的索引→index

  • 元组.index(数据, 开始查找的位置, 结束查找的位置)

tuple_one = (1, 2, 3)
print(tuple_one)
a = tuple_one.index(2)
print(a)

  • 查询元组中tuple_one出现元素2的第一次位置
  • 输出如下:
tuple2.png

2.统计

2.1 获取该数据出现的次数→count

  • 元组.count(数据)

tuple_one = (1, 2, 3, 2, 3, 2)
print(tuple_one)
a = tuple_one.count(2)
print(a)

  • 查询元组tuple_one中元素2出现的次数

  • 输出如下:


    count1.jpg
  • 注:如果要查询的元素不在元组中,则返回0,如下

tuple_one = (1, 2, 3, 2, 3, 2)
print(tuple_one)
a = tuple_one.count(7)
print(a)

  • 查询元组tuple_one中元素7出现的次数
  • 输出如下:
count2.png

2.2 获取元组的长度→len

  • len(元组)

tuple_one = (1, 2, 3, 2, 3, 2)
print(tuple_one)
a = len(tuple_one)
print(a)

  • 查询元组tuple_one的长度
  • 输出如下:
len.png

循环遍历

  • 方法同列表,使用for循环
  • 元组循环遍历中使用格式化字符串不方便,因为一般情况元组中的元素数据类型不一样

元组应用场景

  • 用于函数的参数和返回值
  • 让列表不可修改,安全
  • 格式字符串,%后面的()就是一个数组

列表和元组相互转换

  • 列表转元组

tuple(列表)

  • 元组转列表

list(元组)

字典

字典定义

  • 字典也可以存储多个数据
  • 字典用{}定义
  • 字典无序,列表有序
  • 字典也使用分隔
  • 字典使用键值对存储数据
    1.键key是索引
    2.值value是数据
    3.键和值之间用分隔
    4.键必须唯一
    5.值可以是任意数据类型,但键只能使用字符串、数字或元组
  • 格式如下:

xiaoming = {"name": "小明", "age": 18, "gender": True, "height": 1.75}

  • 注意:书写格式最好一行只有一个键值对
  • 注:因为字典无序,所以控制台输出可能和定义的顺序不一致

字典方法

1.增

  • 字典[key] = value

dict_one = {"name": "zhangsan", "age": 18, "sex": True}
print(dict_one)
dict_one["phone"] = 666
print(dict_one)

  • 在字典dict_one中增加一个键值对phone=666
  • 输出如下:
dict1.jpg

2.删

  • 字典.pop(key)

dict_one = {"name": "zhangsan", "age": 18, "sex": True}
print(dict_one)
dict_one.pop("sex")
print(dict_one)

  • 删除字典dict_one中键位sex的键值对
  • 输出如下:
dict2.png
  • 注:pop方法要删除的键值必须存在,否则报错,如下

dict_one = {"name": "zhangsan", "age": 18, "sex": True}
print(dict_one)
dict_one.pop("phone")
print(dict_one)

  • 删除字典dict_one中键为phone的键值对
  • 输出如下:
dict3.png

3.改

  • 字典[key] = value

dict_one = {"name": "zhangsan", "age": 18, "sex": True}

print(dict_one)
dict_one["sex"] = False
print(dict_one)

  • 修改字典dict_one中的sex键的值为False
  • 输出如下:
dict4.jpg

4.查

  • 字典[key]

dict_one = {"name": "zhangsan", "age": 18, "sex": True}
print(dict_one)
a = dict_one["name"]
print(a)

  • 查询字典dict_one中name键对应的值
  • 输出如下:
dict5.png

5.统计

  • len(字典)

dict_one = {"name": "zhangsan", "age": 18, "sex": True}
print(dict_one)
a = len(dict_one)
print(a)

  • 查询字典dict_one的长度
  • 输出如下:
dict6.png

6.合并

  • 字典1.update(字典2)

dict_one = {"name": "zhangsan", "age": 18, "sex": True}
dict_two = {"name": "lisi", "phone": 77, "email": "114.com"}
print(dict_one)
print(dict_two)
dict_one.update(dict_two)
print(dict_one)
print(dict_two)

  • 将字典2 dict_two的数据追加到字典1 dict_one中,如果数据的键值重复,则取字典2 dict_two中的值
  • 输出如下:
dict7.png

7.清空

  • 字典.clear

dict_one = {"name": "zhangsan", "age": 18, "sex": True}
print(dict_one)
dict_one.clear()
print(dict_one)

  • 将字典dict_one清空数据
  • 输出如下:
dict8.png

字典循环遍历

  • 因字典的value数据类型不确定,所以实际开发,遍历字典需求较少
  • 格式如下:

# for 新定义循环内部使用的key的变量 in 字典
for k in xiaoming:
 print("%s: %s" % (k, xiaoming[k]))

字典和列表应用场景

  • 实际开发更多的应用场景是
    1.使用多个键值对,存储描述一个物体的相关信息
    2.然后将多个这种字典放在一个列表中,再进行遍历,每个循环体内针对每一个字典进行相同的处理
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 200,667评论 5 472
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,361评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 147,700评论 0 333
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,027评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,988评论 5 361
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,230评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,705评论 3 393
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,366评论 0 255
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,496评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,405评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,453评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,126评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,725评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,803评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,015评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,514评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,111评论 2 341

推荐阅读更多精彩内容

  • 1.1 列表的定义 List(列表) 是Python中使用最频繁的数据类型,在其他语言中通常叫做数组 专门用于存储...
    体面_5376阅读 847评论 0 0
  • (一)、主要内容 1.1、列表 1.2、元祖 1.3、字典 1.4、集合列表 、字典、元祖、集合 (二)、列表 2...
    IIronMan阅读 3,539评论 0 7
  • 任务好多,知难就退,好不开心。
    Miss_all_sunday阅读 158评论 1 0
  • 中午放学回来,擦了擦桌子,然后给做的蛋炒饭,吃完饭后自己躺床上看了会书,下午放学回来做完数学试卷,最后两道题不明白...
    单鹏_7f01阅读 36评论 0 0
  • 进入体式: 1. 双脚分开3倍肩宽,两脚内沿平行,脚尖向前。左脚向内收30度,右脚向外旋60度,脚后跟向内收30度...
    洛洱阅读 207评论 0 0