python学习笔记(四)序列的应用

序列概述:序列是python中最基本的数据结构,数据结构就是将数据元素通过特定的方式进行组合的数据集

列表

列表可变的序列,可以储存各种数据类型,用中括号表示,用逗号隔开,列表中的每个元素都有下标。允许元素重复。
name = [" 元素1"," 元素2",“元素3” ] #元素可为任意数据类型

1. 使用append()函数增加列表项

animal = ["老虎","狮子","大象","老鹰"]
animal.append("小鸡")
print(animal)

输出结果

['老虎', '狮子', '大象', '老鹰', '小鸡']

2.insert()函数增加指定列表项:追加指定元素

animal = ["老虎","狮子","大象","老鹰"]
animal.insert(3,"猴子")
print(animal)

输出结果

['老虎', '狮子', '大象', '猴子', '老鹰']

3.extend()函数增加多个列表项:在尾部追加多个元素。

animal = ["老虎","狮子","大象","老鹰"]
animal.extend(["小鸡","小鸟"])
print(animal)

输出结果

['老虎', '狮子', '大象', '老鹰', '小鸡', '小鸟']

4.使用+合并列表项。

animal = ["老虎","狮子","大象","老鹰"]
animal_set = animal+["山羊","青蛙"]
print(animal_set)

输出结果

['老虎', '狮子', '大象', '老鹰', '山羊', '青蛙']

查找列表项

1.使用index()查找列表项

animal = ["老虎","狮子","大象","老鹰"]
i = animal.index("狮子") #返回元素所在下标
#i = animal.index("狮子",2)从下标2开始查
print(i)

输出结果

1

2.成员运算符查找in(存在返回True、不存在返回False)

animal = ["老虎","狮子","大象","老鹰"]
exist_chick = "小鸡" in animal
print(exist_chick)

输出结果

False

3.通过for循环挨个对比,每次取一个元素

animal = ["老虎","狮子","大象","老鹰"]
for i in animal:
    if "小鸡" == i:
        print("存在")
        break

修改列表值

通过对指定下标重新赋值进行修改

animal = ["老虎","狮子","大象","老鹰"]
animal[1] = "小鸡"
print(animal)

输出结果

['老虎', '小鸡', '大象', '老鹰']

删除列表项

1.使用clear()将列表清空

animal = ["老虎","狮子","大象","老鹰"]
animal.clear()
print(animal)

输出结果

[]

2.使用pop函数删除指定下标列表项

animal = ["老虎","狮子","大象","老鹰"]
animal.pop(2)
print(animal)

输出结果

['老虎', '狮子', '老鹰']

3.使用remove()删除指定列表项

animal = ["老虎","狮子","大象","老鹰"]
animal.remove("大象")
print(animal)

输出结果

['老虎', '狮子', '老鹰']

4.del删除指定索引范围的列表

animal = ["老虎","狮子","大象","老鹰"]
del animal[0:2]
print(animal)

输出结果

['大象', '老鹰']

列表分片

通过获取索引的方式,连续获取多个元素就是列表的分片。
切片:列表名[开始下标:结束下标]

animal = ["老虎","狮子","大象","老鹰","小鸟","海豚"]
a = animal[1:3]
print(a)

输出结果

['狮子', '大象']

列表的排序算法—sort排序

使用sort (reverse = False) /sort()升序排序

list1 = [2,345,44,56,0,37,98,34,12]
list2 = ["and","Tfb","Jack","HeroLong","long"]
list1.sort(reverse = False)
list2.sort(reverse = False)
print(list1)
print(list2)

输出结果

[0, 2, 12, 34, 37, 44, 56, 98, 345]
['HeroLong', 'Jack', 'Tfb', 'and', 'long']

使用sort (reverse = True) /sort()降序排序

list1 = [2,345,44,56,0,37,98,34,12]
list2 = ["and","Tfb","Jack","HeroLong","long"]
list1.sort(reverse = True)
list2.sort(reverse = True)
print(list1)
print(list2)

输出结果

[345, 98, 56, 44, 37, 34, 12, 2, 0]
['long', 'and', 'Tfb', 'Jack', 'HeroLong']

反向排序reverse()函数进行排序

list1 = [2,345,44,56,0,37,98,34,12]
list2 = ["and","Tfb","Jack","HeroLong","long"]
list1.reverse() 
list2.reverse()
print(list1)
print(list2)

输出结果

[12, 34, 98, 37, 0, 56, 44, 345, 2]
['long', 'HeroLong', 'Jack', 'Tfb', 'and']

使用sorted()对列表进行临时排序

list1 = [2,345,44,56,0,37,98,34,12]
list2 = ["and","Tfb","Jack","HeroLong","long"]
newlist1 = sorted(list1)
newlist2 = sorted(list2)
print(newlist1)
print(newlist2)

输出结果

[0, 2, 12, 34, 37, 44, 56, 98, 345]
['HeroLong', 'Jack', 'Tfb', 'and', 'long']

使用sort(key = len)按照字符串长度从短到长排序

list2 = ["and","Tfb","Jack","HeroLong","long"]
list2.sort(key = len)
list2.sort(reverse = False)
print(list2)

输出结果

['HeroLong', 'Jack', 'Tfb', 'and', 'long']

使用sort(key = str.lower)大写转小写再排序

list2 = ["and","Tfb","Jack","HeroLong","long"]
list2.sort(key = str.lower)
print(list2)

输出结果

['and', 'HeroLong', 'Jack', 'long', 'Tfb']

二维列表(在列表中还有二级列表)

获取二级列表中的元素

list = [[1,2,4],[5,6,778],[5,6788,9]]
a = list[2][2]
print(a)

输出结果

9

遍历二维列表:将二维列表中的值循环输出

list = [[1,2,4],[5,6,778],[5,6788,9]]
for i in list:
    for j in i:
        print(j)

输出结果

1
2
4
5
6
778
5
6788
9

元组

(元组是不可变的序列,可存储各种数据类型,允许元素重复,不能对元组元素进行增删查改等基本操作)
name = (“小张”,“小王”,“小明”,“小红”)

tuple = 0 #空元组

len(tuple1) #返回一个元组长度

多数据类型元组
tuple1 = (12,True,“hello”)

单个元组

tuple = (12)
print(type(tuple)) #int类型

输出结果

<class 'int'>

单个元组后面要加逗号

tuple = (12,)
print(type(tuple)) #int类型

输出结果

<class 'tuple'>

查找元素

#获取元组的单个元素

name = ("小张","小王","小明","小红")
a = name[0]
print(a)

输出结果

小张

#对元组进行切片

name = ("小张","小王","小明","小红")
a = name[2:4]
print(a)

输出结果

('小明', '小红')

#判断一个元素是否在该元组中

name = ("小张","小王","小明","小红")
for a in name:
    if"小张" == a:
        print("存在")
    break

使用del()函数删除整个元组

name = ("小张","小王","小明","小红")
del(name)
print(name)

输出结果

NameError: name 'name' is not defined

#通过len()函数统计元素的总个数

name = ("小张","小王","小明","小红")
len1 = len(name)
print(len1)

输出结果

4

#用count()函数统计元素出现的次数

name = ("小张","小王","小明","小红","小王")
len1 = name.count("小王")
print(len1)

输出结果

2

合并元素

+表示两个元素的合并操作

name = ("小张","小王")
name2 = ("小明","小红")
newname = name+name2
print(newname)

输出结果

('小张', '小王', '小明', '小红')

字典

1.通过赋值符号定义字典

语法格式

dictionary = {'key':'value','key':'value2','key3':'value3',……,'keyn':'valuen'}

dictionary:表示字典的名称。
key,key2,key3:表示字典的键。
value,value2,value3:表示字典的值。

2.通过映射函数创建字典

语法格式

dictionary = dic(zip(list1,list2))

dictionary:表示字典的名称。
zip()函数:用于将多个列表或元组对应位置的元素组合为元组,并返回包含这些内容的zip对象。
list1:一个列表,用于指定生成字典的键。
list2:一个列表,用于指定生成字典的值。

3.通过给定的关键字参数创建字典
语法格式

dictionary = dict(key = value,key = value2,key3 = value,……,keyn = valuen)

dictionary:表示字典的名称。

练习1

姓名 露西 韩梅梅 李雷 莉莉
年龄 18 20 19 21

建立字典
1.通过赋值符号定义字典

dictionary = {'露西':'18','韩梅梅':'20','李雷':'19','莉莉':'21'}
print(dictionary)

输出结果

{'露西': '18', '韩梅梅': '20', '李雷': '19', '莉莉': '21'}

2.通过映射函数zip()创建字典

name = ['露西','韩梅梅','李雷','莉莉']
age = ['18','20','19','21']
dictionary = dict(zip(name,age))
print(dictionary)

输出结果

{'露西': '18', '韩梅梅': '20', '李雷': '19', '莉莉': '21'}

3.通过给定的关键字参数创建字典

dictionary = dict(露西 = 18,韩梅梅 = 20,李雷 = 19,莉莉 = 21)
print(dictionary)

输出结果

{'露西': 18, '韩梅梅': 20, '李雷': 19, '莉莉': 21}
  1. 输出字典内容: print(dictionary)
    2.根据键输出值 :print(dictionary[“key”])
    3.通过字典对象的get()方法获取指定键的值:

语法格式:dictionary.get(key[,default])

参数说明:
dirtionary:为字典对象,即要从中获取值的字典。
key:为指定的键。
default:可选项,用于指定当指定的“键”不存在时,返回一个默认值 ,如果省略,则返回None。

练习2

dic1 = {“露西”:18,“韩梅梅”:20,“李雷”:19,“莉莉”:21}
请输出韩梅梅的年龄?

(1)根据键输出值 :

dic1 = {"露西":18,"韩梅梅":20,"李雷":19,"莉莉":21}
print(dic1["韩梅梅"])

输出结果

20

(2)通过字典对象的get()方法获取指定键的值:

dic1 = {"露西":18,"韩梅梅":20,"李雷":19,"莉莉":21}
print(dic1.get("韩梅梅"))

输出结果

20

# 遍历字典
使用字典对象的items()方法可以获取字典的“键-值对”列表

语法格式:
dictionary.items()

参数说明:
dircionary:为字典对象。
返回值为可遍历的(键-值对)的元组列表。
可以通过for循环输出键-值对
dictionary.key():返回字典对象的键。
dictionary.values():返回字典对象的值。

dic1 = {“露西”:18,“韩梅梅”:20,“李雷”:19,“莉莉”:21}
1.如何获取字典中的元素?

dic1 = {"露西":18,"韩梅梅":20,"李雷":19,"莉莉":21}
print(dic1.items())

输出结果

dict_items([('露西', 18), ('韩梅梅', 20), ('李雷', 19), ('莉莉', 21)])

2.如何获取具体的每个键和值?

dic1 = {"露西":18,"韩梅梅":20,"李雷":19,"莉莉":21}
for i,j in dic1.items():
print(i,j)

输出结果

露西 18
韩梅梅 20
李雷 19
莉莉 21

添加字典元素

1. 通过赋值添加字典元素:
语法格式:
dictionary[key] = value
参数说明:
dircionary:表示字典的名称。
key:表示字典元素的键,必须是唯一的,并且不可变(字符串,数字或元组)。
value:表示字典元素的值,可以是任何数据类型,不是必须唯一。
如果键已经存在,那么将用新的值替换原来该键的值。(修改)

2. 通过setdefault()函数添加:
语法格式:
dictionary.setdefault(key,value)
参数说明:
dircionary:表示字典的对象。
key:表示字典元素的键,必须是唯一的,并且不可变(字符串,数字或元组)。
value:表示字典元素的值,可以是任何数据类型,不是必须唯一。
如果键已经存在,那么将用新的值替换原来该键的值。(修改)
如果增加仅写一个键,则它的值为None,是一个空值。

字典的查找

1. 通过指定的键找对应的值:
语法格式:
dictionary [key]
参数说明:
dircionary:表示字典的名称。
key:表示字典元素的键。
值存在则输出键对应的值,不存在则会报错。

2. 通过get()查找对象的值:
语法格式:
dictionary.get(key)
参数说明:
dircionary:表示字典的名称。
key:表示字典元素的键。
值存在则输出键对应的值,不存在不会打印输出。

3. 使用in成员操作:
(1)判断字典的键是否存在(调用函数keys())
key in dictionary.keys()
(2)判断字典的值是否存在(调用函数values())
value in dictionary.values()

字典的修改

1. 通过赋值修改字典元素:
语法格式:
dictionary
参数说明:
dircionary:表示字典的名称。
key:表示字典元素的键,必须是唯一的,并且不可变(字符串,数字或元组)。
value:表示字典元素的值,可以是任何数据类型,不是必须唯一。
键存在是修改,键不存在是增加。

**2.通过update()函数实现对键值对更新: **
语法格式:
dictionary.update(dictionary1)
参数说明:
dircionary:表示字典对象。
dircionary1:表示要添加的字典对象。
可以更新,也可以添加一个字典到另一个字典中。将重复的替换,不重复的添加。

  1. 使用del()函数删除:
    语法格式: del(dictionary[key])
  2. 使用pop()函数删除:
    语法格式: dictionary.pop(key)
  3. 使用popitem()函数删除字典中最后一对键值对:
    语法格式: dictionary.popitem()
  4. 使用clear()函数清空所有的键值对:
    语法格式: dictionary.clear()

练习

dic1 = {“露西”:18,“韩梅梅”:20,“李雷”:19,“莉莉”:21}
1.添加23岁露娜的信息到字典中?

方法1:通过赋值添加字典元素

dic1 = {"露西":18,"韩梅梅":20,"李雷":19,"莉莉":21}
dic1["露娜"] = 23
print(dic1)

输出结果

{'露西': 18, '韩梅梅': 20, '李雷': 19, '莉莉': 21, '露娜': 23}

方法2:通过setdefault()函数添加

dic1.setdefault("露娜","23")
print(dic1)

输出结果

{'露西': 18, '韩梅梅': 20, '李雷': 19, '莉莉': 21, '露娜': 23}

方法3:通过update()函数添加

dic1 = {"露西":18,"韩梅梅":20,"李雷":19,"莉莉":21}
dic1.update({"露娜":23})
print(dic1)

输出结果

{'露西': 18, '韩梅梅': 20, '李雷': 19, '莉莉': 21, '露娜': 23}

2.修改李雷的年龄为22岁?

  1. 通过赋值修改字典元素:
dic1 = {"露西":18,"韩梅梅":20,"李雷":19,"莉莉":21}
dic1["李雷"] = 22
print(dic1)

输出结果

{'露西': 18, '韩梅梅': 20, '李雷': 22, '莉莉': 21}

2.通过update()函数完成修改

dic1 = {"露西":18,"韩梅梅":20,"李雷":19,"莉莉":21}
dic1.update({"李雷":22})
print(dic1)

输出结果

{'露西': 18, '韩梅梅': 20, '李雷': 22, '莉莉': 21}

3.输出露西的年龄?
方法1:通过指定的键输出值

dic1 = {"露西":18,"韩梅梅":20,"李雷":19,"莉莉":21}
print(dic1["露西"])

输出结果

18

方法2:通过get()函数完成

dic1 = {"露西":18,"韩梅梅":20,"李雷":19,"莉莉":21}
print(dic1.get("露西"))

输出结果

18

4.露西的信息是否在字典中?张晨晨是否在字典中?如果不在请给出提示信息:张晨晨不在字典中!

dic1 = {"露西":18,"韩梅梅":20,"李雷":19,"莉莉":21}
print("张晨晨"  in dic1)

输出结果

False

5.删除字典中莉莉的信息

方法一:使用del()函数删除

dic1 = {"露西":18,"韩梅梅":20,"李雷":19,"莉莉":21}
del(dic1["莉莉"])
print(dic1)

输出结果

{'露西': 18, '韩梅梅': 20, '李雷': 19}

方法二:使用pop()函数删除

dic1 = {"露西":18,"韩梅梅":20,"李雷":19,"莉莉":21}
dic1.pop("莉莉")
print(dic1)

输出结果

{'露西': 18, '韩梅梅': 20, '李雷': 19}

方法三:popitem()函数删除

dic1 = {"露西":18,"韩梅梅":20,"李雷":19,"莉莉":21}
dic1.popitem()
print(dic1)

输出结果

{'露西': 18, '韩梅梅': 20, '李雷': 19}

列表推导式

1. 生成指定范围的数值列表:
语法格式:
list = [Expression for var in range()]
参数说明:
list:表示生成
的列表名称。
Expression:表达式,用于计算新列表的元素。
var:循环变量。
range:采用range()函数生成的range对象。

2. 根据列表生成指定需要的列表:
语法格式:
newlist = [Expression for var in list]
参数说明:
newlist:表示生成的列表名称。
Expression:表达式,用于计算新列表的元素。
var:循环变量。
list:用于生成新列表的原列表。

练习

1.生成1-30之间奇数的列表。
方法1

numbers = list(range(1,30,2))
print(numbers)
在这里插入代码片

输出结果

[1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29]

方法2

#列表推导式
list = [a for a in range(1,30,2)]
print(list)

输出结果

[1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29]

2.1-100之间的10个随机整数的列表。

方法1

import random   #导入生成随机数的模块
list = []
for a in range(10):
    list.append(random.randint(1,100))#向列表中添加随机数
print(list)

方法2

#列表推导式
import random 
list = [random.randint(1,100) for a in range(10)]
print(list)在这里插入代码片

3.根据第二小题生成的列表中将所有偶数的平方输出。

import random 
list = [random.randint(1,100) for a in range(10)]
newlist = [a*a for a in list]
print(list)
print(newlist)

输出结果

[35, 17, 93, 88, 93, 67, 53, 52, 95, 65]
[1225, 289, 8649, 7744, 8649, 4489, 2809, 2704, 9025, 4225]

4.有列表price = [998,1598,698,2388,688]
(1)请输出商品打6折之后的价目表

price = [998,1598,698,2388,688]
sale = [int(a*0.6) for a in price]
print("原价格:",price)
print("打6折后:",sale)在这里插入代码片

输出结果

原价格: [998, 1598, 698, 2388, 688]6折后: [598, 958, 418, 1432, 412]

(2)请输出价格高于1000的商品列表

price = [998,1598,698,2388,688]
high_price = [a for a in price if a>1000]
print(price)
print("价格高于1000的商品:",high_price)

输出结果

[998, 1598, 698, 2388, 688]
价格高于1000的商品: [1598, 2388]
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值