day6 - 列表和字典
01review
# 增
# 1)列表.append(元素)
nums = [10, 20]
nums.append(100)
print(nums)
# 注意:增删改的操作没有返回值
nums = [10.20]
x = nums.append(100)
print(x) # None
# 练习:提取nums中所有的奇数然后创建一个新的列表
nums = [11, 20, 22, 39, 33, 7, 18, 20]
# [11,39,33,7]
newnums = []
for i in nums:
if i % 2:
newnums.append(i)
print(newnums)
# 练习:输入一个数字,将输入的数字插入到nums中,要求插入后nums仍然是从小到大顺序
# 方法1
nums = [10, 23, 45, 67, 89, 200]
value = int(input())
for index, item in enumerate(nums): # 列举 枚举
if item >= value:
nums.insert(index, value) # 插入是(索引,值)
break
else:
nums.append(value)
print(nums)
# 方法2
nums = [10, 23, 45, 67, 89, 200]
i = 15
for j in range(len(nums)):
if nums[j] >= i:
break
nums.insert(j, i)
print('bbb')
print(nums) # 有局限性没法写在最后面
02列表相关操作
# 1.数学运算符 : +,*
# 列表1 + 聊表2 - 将两个列表合成产生一个新的列表
print([10, 20, 30] + [100, 200]) # 【10,20,30,100,200】
list1 = [10, 20, 30]
list2 = [100, 200]
result = list1 + list2
print(result) # [10,20,30,100,200]
print(list1, list2)
# 列表 *N,N*列表 - N个列表合并成一个新的列表
list1 = [10, 20, 30]
list2 = [100, 200]
print(list2 * 2) # list2 + list2 -》【100,200】 + 【100,200】
print(list1 * 3) # [10,20,30,10,20,30,10,20,30]
# 创建一个列表,列表中有100个None
list3 = [None] * 100
print(list3)
list3.append('a')
print(list3)
"""
输出结果
[None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None]
[None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, 'a']
"""
# 2.比较运算符
比较运算符
# 1)比较是否相等:== , !=
print([10, 20, 30] == [10, 20, 30]) # True
print([10, 20, 30] == [10, 30, 20]) # False
# 2)比较大小 - 两个列表可以比较大小
print([100, 200, 300, 400] > [101, 201]) # False
print([1, 2, 300000, 4000] < [1, 3, -2348384]) # 比较前面的强弱
# print([1, "2", "str", 40000] < [1, -3, -23486384]) # 报错
# 两个列表比较大小,比较的是第一队不相等的元素的大小(相同位置上的元素属于一对)
# 第一对元素相等,后面第二个接着比大小
# 相当于个十百千位的比较方式一样
去重
names = ["张三", "李四", "张三", "王五", "张三", "李四"]
要求输出
# ["张三","李四",”王五“】
# 方法一
names = ["张三", "李四", "张三", "王五", "张三", "李四"]
new_names = []
for x in names:
if x not in new_names:
new_names.append(x)
print(new_names)
# 方法二
names = ["张三", "李四", "张三", "王五", "张三", "李四"]
for _ in range(len(names)):
last_name = names.pop()
if last_name not in names:
names.insert(0, last_name)
print(names)
# 自己的
# 错误的写法
for index1 in range(len(names) - 1):
for index2 in range(index1, len(names) - 1):
if names[index1] == names[index2]:
del names[index2]
print(names)
列表相关函数
# 列表相关函数:sum,max,min,sorted,len,list
# 1.sum(列表)- 求序列中所有元素的和
cores = [29, 23, 89, 90, 99, 83]
print(sum(scores)) # 是数字序列才能统计
# 413
print(sum(range(1, 101)))
# 5050
# 2.
# max(序列) - 求序列中最大的元素
# min(序列) - 求序列中最小的元素
print(max(scores))
print(min(scores))
# 3.
# sorted(序列) - 将序列中的元素从小到大排列,产生一个新的列表
# (序列,reverse = True ) - 将序列中的元素从大到小排序,产生一个新的列表
# .sort()
scores = [19, 23, 89, 90, 99, 83]
new_scores = sorted(scores)
print(new_scores) # [19, 23, 83, 89, 90, 99]]
new_scores = sorted(scores, reverse=True) # reverse 反转的意思
print(new_scores)
# 4.len(序列) - 获取序列中元素的个数
nums = [1,2,3,4,5,6,7]
print(nums) # 7
# 5.list() - 将其他数据转换成列表,将序列中的元素作为列表的元素
# 所有的序列都可以转换成列表
list1 = list("abc123") #列举 一个个列举
print(list1) # ['a', 'b', 'c', '1', '2', '3']
list2 = list(range(3))
print(list2) # [0, 1, 2]
print(type(range(3))) # <class 'range'>
print(type(print)) # print(type(print))
04列表相关方法
# 列表相关方法使用方式:列表,方法名()
# list ctrl + 点击它进入说明
# 1.列表.clear() - 清空列表
nums = [10, 20, 67, 89]
nums.clear()
print(nums) # [] 这个效率高点
效率高于 底下,因为不动内存
nums = [10, 23, 67, 89]
nums = [] #新建空内存?
print(nums)
# 2.列表.copy() - 复制原列表,产生一个一摸一样的新列表
nums = [10, 23, 67, 89]
new_nums1 = nums.copy() # 这是新建了一块内存,同样的量
print("new_nums1:", new_nums1)
新建一块内存地址,赋值一样
new_nums2 = nums # 保存的是同一地址,只是多个标识符
print('new_nums2:', new_nums1)
新建一个指针, 标记地址名
# new_nums1 保存的是新的内存的地址
# new_nums2 改了上边原数据也受到影响
Tip:
好东西
# http://pythontutor.com/visualize.html#mode=edit
[](# http://pythontutor.com/visualize.html#mode=edit)
# 3.列表.count(元素) - 统计指定元素的个数
nums = [10, 20, 30, 10, 20, 10]
print(nums.count(10)) # 3
print(nums.count(20)) # 2
print(nums.count(40)) # 0
# 4.列表.extend(序列) - 将序列中的元素全部添加到列表的最后
nums = [10, 23, 67, 89]
nums.extend('abc') # [10, 23, 67, 89, 'a', 'b', 'c']
# extend延长
print(nums)
extend 是拆了一个一个加,类似于 枚举
a.extend('abc') = a + list(('abc'))
nums = [10, 23, 67, 89]
nums.extend([100, 200]) #[10, 23, 67, 89, 100, 200]
print(nums)
# 5. 列表.index(元素) - 获取指定元素在列表中的下标值(标志的下标值,
# 从0开始的那个下标)
nums = [10, 23, 67, 10, 89]
print(nums.index(10)) # 0
print(nums.index(23)) # 1
# print(nums.index(100)) # 报错
index 通过值来找目录,元素 -》 查到目录 查找语法
# 6.列表.reverse() - 将列表中的元素逆序
nums = [10, 23, 67, 10, 89]
nums.reverse()
print(nums) # [89, 10, 67, 23, 10]
nums = [10, 23, 67, 10, 89]
print(nums[::-1]) # [89, 10, 67, 23, 10]
# 如果原来数据不要了 用切片 切片是个动作
# 切片是动作,但是源列表不变
# 7.
# 列表.sort() - 将列表中的元素从小到大排序 ,对源列表进行更改,没有RETURN
# 列表.sort(reverse = True) - 将列表中的元素从大到小排序
# sorted(列表) - 对列表进行更改,RETURN出新的排序数列,但是原列表不变
# sorted(列表,reverse = True)
nums = [10, 23, 67, 10, 89]
nums.sort() # 在原来容器慢慢排
print(nums) # [10,10,23,67,89]
nums = [10, 23, 67, 10, 89]
new_nums = sorted(nums) # 消耗内存多 效率高 给多容器操作方便
print(new_nums) # [10,10,23,67,89]
列表推导式 (列表遍历式)- 一种创建列表的表达式
"""
结果1:
[表达式 for 变量 in 序列]
[功能 for 变量 in 序列]
[推导式 for 变量 in 序列]
功能:创建一个列表,列表中的元素是表达式的结果: - 让变量
在序列中取值,一个一个的取,每取一个值就计算一次表达式的结果
应用:将原序列中所有的元素,经过统一的变换,转换成新的列表
"""
list1 = [10 for x in 'abc']
print(list1) # [10,10,10]
list2 = [x for x in range(3)]
print(list2) # [0,1,2]
list3 = [x * 2 for x in range(3)]
print(list3) # [0, 2, 4]
list4 = [x % 2 == 0 for x in range(5)]
print(list4) # [True, False, True, False, True]
# 练习: 提取nums中所有元素的个位数
nums = [23, 89, 562, 98, 71, 99]
list5 = [x % 10 for x in nums]
print(list5) # [3, 9, 2, 8, 1, 9]
# 练习2:将nums中所有的元素都乘以10
nums = [23, 89, 562, 98, 71, 99]
list6 = [10 * x for x in nums]
print(list6) # [230, 890, 5620, 980, 710, 990]
money = [19283900, 10203923, 1827300, 67720, 679012]
new_money = [f'{x / 10000:.2f}万' for x in money]
print(new_money)
# ['1928.39万', '1020.39万', '182.73万', '6.77万', '67.90万']
结果2 nums
功能for变量in在区间 if如果满足
[表达式 for 变量 in 序列 if 条件语句]
功能:创建一个列表,列表中的元素是表达式的结果 - 让变量
去序列中取值,一个一个的取,取一个值就判断一次条件语句的结果是否为True
为True就计算表达式的结果
# 应用:提取序列中满足某个条件的元素
print('----------------结构2的用法-----------------')
list1 = [x for x in range(5) if x % 2 == 0]
print(list1) # [0, 2, 4]
nums = [23, 89, 67, 21, 78, 20]
list2 = [x for x in nums if x % 10 < 5]
print(list2) # [23,21,20]
# 练习:用推导式删除scores中小于60的元素
scores = [30, 56, 78, 91, 83, 88, 45, 57, 60]
list3 = [x for x in scores if x >= 60]
print(list3)
# [78, 91, 83, 88, 60]
# 应用:提取序列中满足某个条件的元素
# 练习:将scores中元素全部"及格"或者"不及格"
XX如果 if 条件else YY for 变量 in 区间
scores = [30, 56, 78, 91, 83, 88, 45, 57, 60]
new_scores = ['及格' if x >= 60 else "不及格" for x in scores]
print(new_scores)
# new_scores1 = [x for x in scores "及格" if x >= 60 else "不及格"]
# 不能这样写
# 补充:python中三目运算符
1) C的三目运算符 - ?:
条件语句?值1:值2 - 如果条件语句为真,整个表达式的结果是
值1,否则结果是值2
2) Python的三目运算 - if -else
值1 if 条件语句 else 值2
# 1)C的三目运算符 - ?:
# 条件语句?值1:值2 - 如果条件语句为真,整个表达式的结果是值1,否则结果是值2
x >5?"正确":"错误"
# Python的三目运算 - if -else
# if 条件语句 else 值2
"正确" if x > 5 else "错误"
age = 9
if age >= 18:
print("成年")
else:
print("未成年")
result = "成年" if age >= 18 else "未成年"
print(result)