List in Python

List and For loop in Python

This blog also include some information of the array in Numpy.

Now have a list of [1,-1,1,1,1,-1], want to have another list of category corresponding to the 1 and -1.

1
2
>>> label=[1,-1,1,1,1,-1]
>>> cate =['a' if i==1 else 'b' for i in label]

set value using the filter character in array of numpy

1
2
3
4
5
>>> import numpy as np
>>> label=np.array([1,-1,1,1,1,-1]) # it must be the array
>>> cate=np.empty(len(label))
>>> cate[label==1]=11
>>> cate[label==-1]=-11