arrays - python: creating numpy nonzero index, value pair -


i can index of non-zero numpy arrays follows:

a = np.array([0., 1., 0., 2.]) = np.nonzero(a) 

this returns (array([1, 3]),). can corresponding values as:

v = a[i] 

now create list each of (index, value) tuples. guess 1 way write for loop follows;

l = list() x in range(0, len(v)):     l.append((i[0][x], v[x])) 

however, wondering if there better way without writing loop.

you use list comprehension

l = [(x, v[x]) x in i[0]] 

or use zip other answers suggest


Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -