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
Post a Comment