编程中numpy的一些注意事项(NO.1)
单行索引注意点
a = [1,2,3,4,5,6,7,8,9,10]
a[0:1] = [1]
包含开头,不包含结尾。
a [:-1]: 从头一直到最后一个元素a[-1],但不包含最后一个元素
#要索引从某一个位置到结束点的方法
a[3:]#表示的是从index=3 ,一直到结束的所有元素
Python中的判断加逻辑的使用
def tt(a,b):
trend = []
if b == 0 or b == 3 or b == 7 or b == 4 or (a == 0 or a ==1 or a == 5 and b == 2) or (a == 5 and b == 6):
trend.append(1)
elif b == 5 or (a ==2 or a ==3 or a ==4 and b == 6) or (a != 5 and b == 1):
trend.append(-1)
return trend
特别的:如果写成这种,只执行了一次
if b == 0 or 3 or 7 :
pass
这种就只执行了b == 0 的情况
或且非的含义:
或(or):表示 a != 1 or a != 2 :当a != 1 是为true ,当a != 2时为true,
只有当a = 1 且同时 = 2时候才为false
需要特别注意的点,a!=2 and a!=3 这个时候才能过滤掉a = 2 和 a = 3
numpy数组与list之间的转换
将a转化为numpy的array:
a=([3.234,34,3.777,6.33])
a为python的list类型
np.array(a)
array([ 3.234, 34. , 3.777, 6.33 ])
将a转化为python的list
a.tolist()