aestrivex
aestrivex

Reputation: 5280

iterating over a tuple of iterables returned from a function

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)):

Upvotes: 1

Views: 54

Answers (1)

user2555451
user2555451

Reputation:

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)

Upvotes: 2

Related Questions