python列表集合中常用的小技巧

# 集合可以进行合并补操作,很有用 [无序]
B = {4, 5, 6, 7}
A = {1, 2, 3, 4}
C = A | B  # 并集 {1,2,3,4,5,6,7}
D = A & B  # 交集 {4}
E = B - A  # {5,6,7}
# 实现列表对应元素相加
# 列表可以使用列表推导式
a = [1, 2, 3, 4]
b = [item*2 for item in a]  # [2,4,6,8]
c = list(map(lambda x: x*2, b))  # [2,4,6,8]
myList = [item-4 for item in a if item > 4]  # [2,4]
# numpy 中mat()==matrix()
import numpy as np
dd = np.mat([4, 6, 5])
rank = dd.argsort()  # [0,2,1] 返回index排序[从小到大]
ee = np.mat([2, 1, 1])
ff = np.multiply(dd, ee)  # 对应matlab点乘 [8,6,5]

猜你喜欢

转载自blog.csdn.net/qq_40801709/article/details/100106412