python学习笔记 深拷贝和浅拷贝和列表和字符串的区别

#深拷贝和浅拷贝

#浅拷贝:复制过程中,只复制一层变量,不会复制深层变量绑定的对象

# list01 = [800,[1000,500]]

# list01[1][0] = 900

# list02 = list01.copy()

# print(list02)

#深拷贝:复制整个依赖的变量.

# import copy

# list02 = copy.deepcopy(list01)

# print(list02)

#练习1,将列表[54,25,12,42,35,17]中,大于30的数字存在另外一个列表

# list01= [54,25,12,42,35,17]

# list02= []

# number = 30

# for i in range(0,len(list01)):

# if list01[i] > number:

# list02.append(list01[i])

# print(list02)

#练习2:在控制台输入5个数字,打印最大值(不使用max)

# max = 0

# for i in range(5):

# list01_input = int(input(("请输入第%d个数字")%(i+1)))

# if max < list01_input:

# max = list01_input

# print(max)

#练习3:将列表[54,25,12,42,35,17],打印最大值(不使用max)

# list01= [54,25,12,42,35,17]

# for i in range(0,len(list01)):

# if list01[0] < list01[i]:

# list01[0] = list01[i]

# print(list01[0])

#练习3:将列表[54,25,12,42,35,17],删除大于20的数字。

# list01= [54,25,12,42,35,17]

# for i in range(len(list01)-1,-1,-1):

# if list01[i] > 20:

# list01.remove(list01[i])

# print(list01)

#列表VS字符串

# 1.列表和字符串都是序列,元素之间有先后顺序关系。

# 2.字符串是不可变的序列,列表是可变的序列

# 3.字符串中每个元素只能存储字符,而列表可以存储任意类型

# 4.列表和字符串都是可迭代对象

# 5.函数:

#将多个字符串拼接为一个。

# result ="拼接符".join(列表)

#需求:根据xx逻辑,拼接一个字符串

#生成0123456789数并且是字符串

#优点:每次循环只向列表添加字符串,没有创建列表对象

# list_temp = []

# for item in range(10):

# list_temp.append(str(item))

# result =''.join((list_temp))

# print(result)

#缺点:每次循环形成一个新的字符串对象,替换变量引用result。

#第一次item ""

#第二次item "0"

#第三次item "01"

# .......

# result = ""

# for item in range(10):

# result += str(item)

# print(result)

#练习:在控制台中循环输入字符串,如果输入框则停止

#最后打印所有内容(拼接后的字符串)

# list_temp = []

# while True:

# str_input = input("输入字符串")

# if str_input == "":

# break

# list_temp.append(str_input)

# str = "".join(list_temp)

# print(str)

#将一个字符串拆分成多个

#列表= "a-b-c-d".split('分隔符')

# str01 = "1-2-3"

# list_str01 = str01.split("-")

# print(list_str01)

#练习:英文单词翻转

# "how are you" ----- "you are hou"

# str01 = "how are you"

# list_str01 = str01.split(" ")

# str_result = " ".join(list_str01[::-1])

# print(str_result)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容