Change the values of a NumPy array that are NOT in a list of indices

前端 未结 4 1085
小鲜肉
小鲜肉 2020-12-05 13:26

I have a NumPy array like:

a = np.arange(30)

I know that I can replace the values located at positions indices=[2,3,4] using f

相关标签:
4条回答
  • 2020-12-05 13:31

    I don't know of a clean way to do something like this:

    mask = np.ones(a.shape,dtype=bool) #np.ones_like(a,dtype=bool)
    mask[indices] = False
    a[~mask] = 999
    a[mask] = 888
    

    Of course, if you prefer to use the numpy data-type, you could use dtype=np.bool_ -- There won't be any difference in the output. it's just a matter of preference really.

    0 讨论(0)
  • 2020-12-05 13:32

    Only works for 1d arrays:

    a = np.arange(30)
    indices = [2, 3, 4]
    
    ia = np.indices(a.shape)
    
    not_indices = np.setxor1d(ia, indices)
    a[not_indices] = 888
    
    0 讨论(0)
  • 2020-12-05 13:38

    Just overcome similar situation, solved this way:

    a = np.arange(30)
    indices=[2,3,4]
    
    a[indices] = 999
    
    not_in_indices = [x for x in range(len(a)) if x not in indices]
    
    a[not_in_indices] = 888
    
    0 讨论(0)
  • 2020-12-05 13:53

    Obviously there is no general not operator for sets. Your choices are:

    1. Subtracting your indices set from a universal set of indices (depends on the shape of a), but that will be a bit difficult to implement and read.
    2. Some kind of iteration (probably the for-loop is your best bet since you definitely want to use the fact that your indices are sorted).
    3. Creating a new array filled with new value, and selectively copying indices from the old one.

      b = np.repeat(888, a.shape)
      b[indices] = a[indices]
      
    0 讨论(0)
提交回复
热议问题