<div dir="ltr"><br><div class="gmail_extra"><br><br><div class="gmail_quote">On Fri, Jul 5, 2013 at 4:20 AM, Bakhtiyor Zokhidov <span dir="ltr"><<a href="mailto:bakhtiyor_zokhidov@mail.ru" target="_blank">bakhtiyor_zokhidov@mail.ru</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<div><p>Hi everybody,<br><br>I have a problem with sorting out the following function. What I expect is that I showed as an example below.<br><br>Two problems are encountered to achieve the result:<br>1) The function sometimes can't not sort as expected: I showed an example for that below.<br>
2) I could not do vectorization to avoid loop.<br><br><br>OR, Is there another way to solve that problem??<br>Thanks in advance<br><br><br>Example:<br>data = ['', 12, 12, 423, '1', 423, -32, 12, 721, 345]. Expected result:  [0, 12, 12, 423, 0, 423, -32, 12, 721, 345], <span style="font-size:12px">here, '' and '1' are string type I need to replace them by zero</span><br>
</p></div></blockquote><div><br></div><div>I don't understand your code example, but if your problem is fully described as above (replace the strings '' or '1' with the integer 0), then it would seem simplest to just do this with python built in functions rather than using numpy. The numpy functions work best with arrays, and your "data" variable is a python list with a mixture of integers and strings.<br>
<br></div><div>Here is a possible solution:<br><br>>>> data = ['', 12, 12, 423, '1', 423, -32, 12, 721, 345]<br>>>> foo = lambda x: 0 if (x == '') or (x == '1') else x<br>
>>> print map(foo, data)<br>[0, 12, 12, 423, 0, 423, -32, 12, 721, 345]<br><br><br></div><div>Hope that helps,<br></div><div>Aronne<br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<div><p>The result I got: ['', 12, 12, 423, '1', 423, -32, 12, 721, 345]<br><br><span style="font-size:12px">import numpy as np</span></p><p>def func(data):</p><p>          x, i = np.unique(data, return_inverse = True)<br>
          f = [ np.where( i == ind )[0] for ind in range(len(x)) ]</p><p>          new_data = []<br>          # Obtain 'data' arguments and give these data to New_data<br>          for i in range(len(x)):<br>                      if np.size(f[i]) > 1:<br>
                                 for j in f[i]:<br>                                         if str(data[j]) <> '':<br>                                                    new_data.append(data[j])<br>                                              else:<br>
                                                    data[j] = 0<br>          return data</p><span class=""><font color="#888888"><p><span style="font-size:12px">-- </span></p>Bakhtiyor Zokhidov</font></span></div>
<br>_______________________________________________<br>
NumPy-Discussion mailing list<br>
<a href="mailto:NumPy-Discussion@scipy.org">NumPy-Discussion@scipy.org</a><br>
<a href="http://mail.scipy.org/mailman/listinfo/numpy-discussion" target="_blank">http://mail.scipy.org/mailman/listinfo/numpy-discussion</a><br>
<br></blockquote></div><br></div></div>