python 实用命令

列表理解:

list = [-1, -2, 1, 2]
[item for item in list if item > 0]
[1, 2]
d = {'person': 2, 'cat': 4, 'spider': 8}
[animal for animal in d if d[animal] > 4]

求满足某条件的元素的下标

nums = [0, 1, 2, 3, 4]
even_num_to_square = {x: x ** 2 for x in nums if x % 2 == 0}
indice = [i for i, elem in enumerate(nums) if elem >3]

求norm

np.linalg.norm(array)
or
from scipy.linalg import norm

reference

posted @ 2017-03-06 20:27  fariver  阅读(259)  评论(0编辑  收藏  举报