python_day05(笔记及练习)

本文深入探讨Python列表的各种操作技巧,包括元素增删、排序、查找最大最小值、深浅拷贝的区别及应用,以及如何通过列表推导式简化代码。同时,文章提供了丰富的示例,如成绩管理、姓名录入、斐波那契数列生成等,帮助读者掌握列表在实际编程中的高效运用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

"""
    列表内存图
"""
list01 = ["张无忌","赵敏"]
list01.append("小昭")
list01.insert(1,"周芷若")
list01.remove("赵敏")

深拷贝和浅拷贝
浅拷贝:复制过程中,只复制一层变量,不会复制深层变量绑定的对象的复制过程。
深拷贝:复制整个依懒的变量。

"""
    深拷贝

    优点:
        对其中之一的修改,
        绝对不影响另外一个.
    缺点:
        比较占用内存
"""
# 准备深拷贝的工具
import copy

list01 = [
    [1,2,3],
    [4,5,6]
]
# list02 =list01[:]
# 深拷贝列表
list02 = copy.deepcopy(list01)
list01[0] = "一"
list01[1][0] = "二"
print(list02)#
"""
    list01 = [4,5,65,76,7,8]
    在列表中找出最大值
    算法:
        假设第一个是最大值
        如果第二个大于假设的,则替换假设的.
        如果第三个大于假设的,则替换假设的.
        如果第四个大于假设的,则替换假设的.
        最后输出假设的(最大的)
"""
list01 = [4, 5, 65, 76, 7, 8]
max_value = list01[0]

for i in range(1, len(list01)):
    if max_value < list01[i]:
        max_value = list01[i]
print(max_value)
# for item in list01:
#     if max_value < item:
#         max_value = item

列表VS字符串
1. 列表和字符串都是序列,元素之间有先后顺序关系。
2. 字符串是不可变的序列,列表是可变的序列。
3. 字符串中每个元素只能存储字符,而列表可以存储任意类型。
4. 列表和字符串都是可迭代对象。
5. 函数:
将多个字符串拼接为一个。
result = “连接符”.join(列表)

"""
    str --> list
    练习:exercise08
"""

line = "孙悟空-八戒-唐三藏"
list_person = line.split("-")
print(list_person)

将一个字符串拆分为多个。
列表 = “a-b-c-d”.split(“分隔符”)
列表推导式
1. 定义:
使用简易方法,将可迭代对象转换为列表。
2. 语法:
变量 = [表达式 for 变量 in 可迭代对象]
变量 = [表达式 for 变量 in 可迭代对象 if 条件]
3. 说明:
如果if真值表达式的布尔值为False,则可迭代对象生成的数据将被丢弃。

"""
    列表推导式
        根据一个可迭代对象,构建另外一个列表。
    练习:exercise09
"""
# 需求1:将list01中每个元素增加10之后,存入list02
list01 = [34, 45, 54, 65, 67, 87]
# list02 = []
# for item in list01:
#     list02.append(item + 10)
list02 = [item + 10 for item in list01]

print(list02)

# 需求2:将list01中所有偶数增加10之后,存入list02
# list02 = []
# for item in list01:
#     if item % 2 == 0:
#         list02.append(item + 10)
list02 = [item + 10 for item in list01 if item % 2 == 0]
"""
    list --> str
    练习:exercise07
"""
# 根据某些逻辑,拼接字符串.
list01 = ["3", "4", "5", "7"]
# str_result = ""
# for item in list01:
#     # 每次循环,每次拼接,每次创建新对象
#     str_result = str_result + item
# print(str_result)
str_result = "——".join(list01)
print(str_result)

身份运算符:

"""
    身份运算符
"""
list01 = [100]
list02 = [100]

# 两个变量指向的对象是否同一个
print(list01 is list02)# False

list03 = list01
print(list03 is list01)# True

# 原理:判断变量存储的地址是否相同
print(id(list01))# 140235347048584
print(id(list02))# 140235357765192
print(id(list03))# 140235347048584

“”"
在终端中循环录入学生成绩,如果录入空,则停止。
打印最高分、最低分、平均分.
体会:容器
“”"
list_scores = []
while True:
str_score = input(“请输入成绩:”)
if str_score == “”:
break
list_scores.append(int(str_score))

print(“最高分:%d” % max(list_scores))
print(“最低分:%d” % min(list_scores))
print(“平均分:%f” % (sum(list_scores) / len(list_scores)))

"""
    在终端中循环录入学生姓名,如果录入空,则停止.倒序输出所有人。
    要求:姓名不能重复(如果重复提示,不存储.)
"""
list_names = []
while True:
    name = input("请输入姓名:")
    if name == "":
        break
    if name not in list_names:
        list_names.append(name)
    else:
        print(name + "已经存在")

for i in range(len(list_names) - 1, -1, -1):
    print(list_names[i])
"""
    list01 = [54,5,65,67,78,8]
    删除列表中所有奇数
    三板斧:内存图  调试
    15:25
"""

list01 = [54, 5, 65, 67, 78, 8]
# for item in list01:
#     if item % 2:
#         list01.remove(item)

# 结论:在列表中删除多个元素,倒序删除。
for i in range(len(list01)-1,-1,-1):
    if list01[i] % 2:
        del list01[i]

print(list01)
"""
    斐波那契数列:从第三项开始,每一项都等于前两项之和.
        1,1,2,3,5,8,....
    获取一个斐波那契数列长度,打印列表.
"""
lenght = int(input("请输入斐波那契数列长度:"))
# 6
list_sequence = [1,1]
# 0
# 1
# 2 -->  2-2  2-1
# 3 -->  3-2  3-1
# ? -->  ?-2  ?-1

# for i in range(2,lenght):# 2 3 4 5
#     element = list_sequence[i-2] +  list_sequence[i-1]
#     list_sequence.append(element)

for __ in range(lenght - 2):
    # 当前元素 = 最后两个元素之和
    element = list_sequence[-2] +  list_sequence[-1]
    list_sequence.append(element)
print(list_sequence)
# 在终端中循环录入字符串,如果录入为空,则停止.
# 打印所有录入的内容(一个字符串)

# 核心思想:使用可变对象代替不可变对象,进行频繁操作.
list_result = []
while True:
    content = input("请输入内容:")
    if content == "":
        break
    list_result.append(content)

str_result =  "".join(list_result)
print(str_result)
"""
    将英文语句进行反转
    How are you   -->  you are How
"""
message = "How are you"
list_temp = message.split(" ")
str_result = " ".join(list_temp[::-1])
print(str_result)
# 练习1:使用列表推导式生成1--50之间能被3或者5整除的数字
list01 = [item for item in range(1, 51) if item % 3 == 0 or item % 5 == 0]
print(list01)

# 练习2:使用列表推导式生成5--60之间数字的平方
list02 = [item ** 2 for item in range(5, 61)]
print(list02)

# 练习3:将1970年到2050年之间的闰年存入列表
list03 = []
for year in range(1970, 2051):
    if year % 4 == 0 and year % 100 != 0 or year % 400 == 0:
        list03.append(year)
# list03 = [year for year in range(1970,2051) if year % 4 ==0 and year % 100 !=0 or year % 400 == 0 ]
print(list03)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值