目录
#去掉列表(元组)中重复元素
a = [1, 2, 2, 3, 4]
a_result = list(set(a))
x1 = {1, 2, 3}
x2 = {3, 4, 5}
#并集
print(x1 | x2) # 或者x1.union(x2)
#交集
print(x1 & x2) # 或者x1.intersection(x2)
#其他操作
print(x1 - x2) # x1.difference(x2) x1中有但x2中没有
print(x1 ^ x2) # 在x1和x2中不同时都有的
#连接列表:+和extend 连接元组:+
a = [1, 5, 7, 9, 6]
b = [2, 3, 3, 6, 8]
print(a + b)
[1, 5, 7, 9, 6, 2, 3, 3, 6, 8]
a.extend(b)
print(a)
[1, 5, 7, 9, 6, 2, 3, 3, 6, 8]
#差异
'''
1.+不会改变参与连接列表的值,但extend会改变a的值
2.+两侧要么都是元组要么都是列表,但列表的extend方法可以将一个元组添加到列表后
'''
import random
#方法一
def random_list1(a):
for i in range(0, 100):
index1 = random.randint(0, len(a) - 1)
index2 = random.randint(0, len(a) - 1)
a[index1], a[index2] = a[index2], a[index1]
return a
#方法二
def random_list2(a):
a_copy = a.copy()
result = []
for i in range(0, len(a)):
index = random.randint(0, len(a_copy) - 1)
result.append(a_copy[index])
a_copy.pop(index)
return result
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
b = random_list1(a)
c = random_list2(a)
print(b)
print(c)
#第二题
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
random.shuffle(a)
#单星(*)
#以元组形式导入,可变参数
#如果最后一个参数不是可变参数,那么为可变参数后面的形参指定参数值,必须用命名参数
def fun1(param1, *param2):
print('param1:', param1)
print('param2:', param2, type(param2))
fun1(1, 2, 3, 4, 5)
param1: 1
param2: (2, 3, 4, 5) <class 'tuple'>
#双星(**)
#以字典形式导入 key 和 value
def fun2(param1, **param2):
print('param1:', param1)
print('param2:', param2, type(param2))
fun2(1, a = 2, b = 3, c = 4, d = 5)
param1: 1
param2: {'a': 2, 'b': 3, 'c': 4, 'd': 5} <class 'dict'>
#合并列表和字典
a = [1, 2, 3]
b = [4, 5, 6]
x1 = [a, b] #[[1, 2, 3], [4, 5, 6]]
x2 = [*a, *b] #[1, 2, 3, 4, 5, 6]
a = {'A': 1, 'B': 2}
b = {'C': 3, 'D': 4}
x = {**a, **b} # {'A': 1, 'B': 2, 'C': 3, 'D': 4}
a = {'A': 1, 'B': 2}
print({v: k for k, v in a.items()})
{1: 'A', 2: 'B'}
#第二题
a = [i for i in range(101)]
#列表
a = ['a', 'b']
b = [1, 2]
print(dict(zip(a, b)))
{'a': 1, 'b': 2}
#元组
fields = ('id', 'name', 'age')
records = [['01', 'Bill', '20'], ['02', 'Mike', '30']]
result = []
'''
[
{'id': '01', 'name': 'Bill', 'age': '20'}
{'id': '02', 'name': 'Mike', 'age': '30'}
]
'''
for record in records:
result.append(dict(zip(fields, record)))
print(result)
[{'id': '01', 'name': 'Bill', 'age': '20'}, {'id': '02', 'name': 'Mike', 'age': '30'}]
#4个区别
#1:语法差异
a = (1, 2, 3)
b = [1, 2, 3]
#2:元组是只读的,列表可读写
#3:
copy_a = tuple(a)
copy_b = list(b)
print(a is copy_a) # True
print(b is copy_b) # False
#4:大小不同
print(a.__sizeof__()) #48
print(b.__sizeof__()) #64
#排序列表的方法
a = [3, 2, 5, 6, 7, 9, 11]
a.sort()
print(a) # [2, 3, 5, 6, 7, 9, 11]
b = [3, 2, 5, 6, 7, 9, 11]
c = sorted(b)
print(b) # [2, 3, 5, 6, 7, 9, 11]
'''
区别:
1.sort属于列表方法,sorted是独立函数
2.sort改变列表本身,sorted返回一个排好序的列表副本
'''
#倒叙排列列表
a.sort(reverse= True)
print(a)
print(sorted(b, reverse= True))
#方法一:Magic方法
class MyClass:
def __init__(self):
self.value = 0
#定义大于号
def __gt__(self, other):
return self.value > other.value
'''
#定义小于号
def __lt__(self, other):
return self.value < other.value
'''
my1 = MyClass()
my1.value = 20
my2 = MyClass()
my2.value = 10
my3 = MyClass()
my3.value = 30
a = [my1, my2, my3]
print(a)
a.sort()
print(a[0].value) #10
print(a[1].value) #20
print(a[2].value) #30
#方法二:operator模块
class MyClass:
def __init__(self):
self.value = 0
my1 = MyClass()
my1.value = 20
my2 = MyClass()
my2.value = 10
my3 = MyClass()
my3.value = 30
a = [my1, my2, my3]
print(a)
import operator
a.sort(key= operator.attrgetter('value'))
b = sorted(a, key= operator.attrgetter('value'))
#第一题
print(a[0].value) #10
print(a[1].value) #20
print(a[2].value) #30
print(b[0].value) #10
#第二题
#Magic方法
class MyClass:
def __init__(self):
self.value = 0
'''
def __gt__(self, other):
return self.value > other.value
'''
def __lt__(self, other):
return self.value > other.value #此处<号改为>号
my1 = MyClass()
my1.value = 20
my2 = MyClass()
my2.value = 10
my3 = MyClass()
my3.value = 30
a = [my1, my2, my3]
print(a)
a.sort()
print(a[0].value) #30
print(a[1].value) #20
print(a[2].value) #10
#reverse= True
a.sort(key= operator.attrgetter('value'), reverse= True)
b = sorted(a, key= operator.attrgetter('value'), reverse= True)