[Numpy-discussion] Unique() function and avoiding Loop

Bakhtiyor Zokhidov bakhtiyor_zokhidov at mail.ru
Fri Jul 5 04:20:28 EDT 2013


Hi everybody,

I have a problem with sorting out the following function. What I expect is that I showed as an example below.

Two problems are encountered to achieve the result:
1) The function sometimes can't not sort as expected: I showed an example for that below.
2) I could not do vectorization to avoid loop.


OR, Is there another way to solve that problem??
Thanks in advance


Example:
data = ['', 12, 12, 423, '1', 423, -32, 12, 721, 345]. Expected result:  [0, 12, 12, 423, 0, 423, -32, 12, 721, 345],  here, '' and '1' are string type I need to replace them by zero

The result I got: ['', 12, 12, 423, '1', 423, -32, 12, 721, 345]

import numpy as np
def func(data):
          x, i = np.unique(data, return_inverse = True)
          f = [ np.where( i == ind )[0] for ind in range(len(x)) ]
          new_data = []
          # Obtain 'data' arguments and give these data to New_data
          for i in range(len(x)):
                      if np.size(f[i]) > 1:
                                 for j in f[i]:
                                         if str(data[j]) <> '':
                                                    new_data.append(data[j])
                                              else:
                                                    data[j] = 0
          return data
--  Bakhtiyor Zokhidov
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20130705/ae9cd58f/attachment.html>


More information about the NumPy-Discussion mailing list