python入门(4)
拷贝
引用赋值
list1=[1,2,3,[4,5]]
list2=list1
print("list1:",list1)
print("list2:",list2)
list1[0]=100
print("list1:",list1)
print("list2:",list2)
# list1: [1, 2, 3, [4, 5]]
# list2: [1, 2, 3, [4, 5]]
# list1: [100, 2, 3, [4, 5]]
# list2: [100, 2, 3, [4, 5]]
浅拷贝
list自带的拷贝
list1=[1,2,3,[4,5]]
list2=list1.copy()
print("id1",id(list1))
print("id2",id(list2))
# id1 2658683478600
# id2 2658713014344
- 拷贝完成之后的列表,与原始列表,拥有不同的内存地址
list1=[1,2,3,[4,5]]
list2=list1.copy()
print("list1:",list1)
print("list2:",list2)
list1[0]=100
print("list1:",list1)
print("list2:",list2)
# list1: [1, 2, 3, [4, 5]]
# list2: [1, 2, 3, [4, 5]]
# list1: [100, 2, 3, [4, 5]]
# list2: [1, 2, 3, [4, 5]]
list1=[1,2,3,[4,5]]
list2=list1.copy()
print("list1:",list1)
print("list2:",list2)
list1[3][1]=100
print("list1:",list1)
print("list2:",list2)
print("id1_",id(list1[3]))
print("id2_",id(list2[3]))
'''
list1: [1, 2, 3, [4, 5]]
list2: [1, 2, 3, [4, 5]]
list1: [1, 2, 3, [4, 100]]
list2: [1, 2, 3, [4, 100]]
id1_ 2206003192328
id2_ 2206003192328
'''
- 子对象是同一个,修改子对象,另外一个列表会发生变化
深拷贝
from copy import deepcopy
list1=[1,2,3,[4,5]]
list2 = deepcopy(list1)
print("list1",list1)
print("list2",list2)
print("list1_id",id(list1))
print("list2_id",id(list2))
print("####修改#########")
list1[1]=100
print("list1",list1)
print("list2",list2)
print("####修改子对象#########")
list1[3][0]=200
print("list1",list1)
print("list2",list2)
print("list1_id",id(list1[3]))
print("list2_id",id(list2[3]))
'''
list1 [1, 2, 3, [4, 5]]
list2 [1, 2, 3, [4, 5]]
list1_id 3105381343496
list2_id 3105381441608
####修改#########
list1 [1, 100, 3, [4, 5]]
list2 [1, 2, 3, [4, 5]]
####修改子对象#########
list1 [1, 100, 3, [200, 5]]
list2 [1, 2, 3, [4, 5]]
list1_id 3105381344520
list2_id 3105381441672
'''
- id地址不同,子对象也是不同的
- 深拷贝会创建一个跟原来列表完全没有关系的列表,他们的列表对象和子对象都是不同的,他们的操作也是不同的,是完全独立的
列表排序
sort()
def sort(self, key=None, reverse=False): # real signature unknown; restored from __doc__
""" L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE* """
# 无返回值,对列表进行排序,reverse表示从大到小,还是从小到大
pass
list1=[1,3,234,61,4,]
print(list1)
list1.sort()
print(list1)
list1.sort(reverse=True)
print(list1)
'''
[1, 3, 234, 61, 4]
[1, 3, 4, 61, 234]
[234, 61, 4, 3, 1]
'''
-
无返回值,在原数组
-
默认从小到大
-
参数reverse=True,为从大到小
sorted()
def sorted(*args, **kwargs): # real signature unknown
"""
Return a new list containing all items from the iterable in ascending order.
A custom key function can be supplied to customize the sort order, and the
reverse flag can be set to request the result in descending order.
"""
pass
返回一个新的列表
逆置
reverse()
def reverse(self): # real signature unknown; restored from __doc__
""" L.reverse() -- reverse *IN PLACE* """
pass
- 无返回值,在当前的数据上做操作
reversed()
class reversed(object):
"""
reversed(sequence) -> reverse iterator over values of the sequence
Return a reverse iterator
"""
- 返回一个迭代器对象,通过for循环获取
生成随机数
def randint(self, a, b):
"""Return random integer in range [a, b], including both end points.
"""
return self.randrange(a, b+1)
唯一一个左闭右闭
练习:10个人分3个班级
list_=[[],[],[]]
from random import randint
for i in range(1,11):
get_r=randint(0,2)
list_[get_r].append(i)
print(list_)
# [[1, 4, 8, 9], [7], [2, 3, 5, 6, 10]]