1
ci0 = np.random.randint(10,size=(15,))

uq,ix = np.unique(ci0, return_index=True):
for i,u in zip(uq,ix):
  print i,u

Is there a nice pythonic way to do this in one line? Specifically, iterate over the results of np.unique (returned as a tuple).

It seems like there should be, but the only solution I could think of is this, which I think is too obfuscating to be elegant:

for i,u in np.transpose(np.unique(ci0, return_index=True)):
aestrivex
  • 5,170
  • 2
  • 27
  • 44

1 Answers1

2

You could use argument unpacking (splatting):

ci0 = np.random.randint(10,size=(15,))

for i,u in zip(*np.unique(ci0, return_index=True)):
  print i,u

To explain better, consider the following code:

func(*(1, 2, 3))

It is equivalent to this:

func(1, 2, 3)
Community
  • 1
  • 1