1

i have a numpy array as

text = [[...],[...],[...],[...]]

number of elements in text is 4 X 80.

Values = [[...],[...],[...],[...]]

Number of elements in values is 4 X 80

Now What I want to do is remove all the spaces at the last after the string characters ends in the text text[3] block and I want to remove the corresponding index in values as well,

How to attain this in fastest way?

I want it to generalised so when elements increase , it should work.

Updating code while learning from net

** Now how to delete those 30 elements from last**

import numpy as np

a = np.array([[...],[...],[...],[...]])
count = 0
for elem in reversed(a[len(a)-1]):
    if elem == ' ':
        count = count + 1
        #new = np.delete(a[len(a)-1], np.where(elem == ' '), axis=0)
    if elem != ' ':
        break
10
  • 1
    what do you mean by after string character ends? Commented Jul 15, 2018 at 10:14
  • In the text[3] the last elemts are appended with spaces only which needs to be removed Commented Jul 15, 2018 at 10:21
  • what do you mean by removing corresponding index? can 0 or ' ' be put there. otherwise shape will no longer remain same. Commented Jul 15, 2018 at 10:27
  • Right now the shapes are same so when last appending spaces will be removed as well as their corresponding index in values the shape will be same na Commented Jul 15, 2018 at 10:30
  • 1
    if we are just to update text 3, you count the last values that are " " ie 30 and delete them. or simply pop them out until you get to a character. then stop. Count how many you popped out and delete the same number from the calues Commented Jul 15, 2018 at 10:32

2 Answers 2

1

Write a function that only pops out the last spaces:

 def mm(a,b):
       v = a.pop()
       if v==" ":
           b.pop()
           mm(a,b)
       else: a.append(v)

Now run the code on your lists / It will be good to run on a small data to ensure it runs before you run this on your whole data as it modifies the original data/ calling by reference.

[mm(x,y) for x,y in zip(text,Values)]  

Now what is the size of the remaining list?

[len(x) for x in Values]
[80, 80, 80, 50]

[len(x) for x in text]
[80, 80, 80, 50]

The code only impacted the third list element

Sign up to request clarification or add additional context in comments.

Comments

1

At the first step, use where get what you want.

In [20]: np.where(a !=' ')
Out[20]: 
(array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2,
        2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
        2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
        2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
        3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
        3, 3, 3, 3]),
 array([ 1,  3,  4,  6,  7,  8,  9, 11, 13, 14, 16, 17, 18, 19, 21, 23, 24,
        26, 27, 28, 29, 31, 33, 34, 36, 37, 38, 39, 41, 43, 44, 46, 47, 48,
        49, 51, 53, 54, 56, 57, 58, 59, 61, 63, 64, 66, 67, 68, 69, 71, 73,
        74, 76, 77, 78, 79,  1,  3,  4,  6,  7,  8,  9, 11, 13, 14, 16, 17,
        18, 19, 21, 23, 24, 26, 27, 28, 29, 31, 33, 34, 36, 37, 38, 39, 41,
        43, 44, 46, 47, 48, 49, 51, 53, 54, 56, 57, 58, 59, 61, 62, 63, 64,
        65, 66, 67, 68, 69, 71, 73, 74, 76, 77, 79,  1,  3,  5,  6,  7,  8,
        10, 11, 12, 13, 15, 16, 17, 19, 21, 22, 24, 25, 26, 27, 28, 29, 30,
        31, 32, 33, 34, 35, 36, 37, 38, 40, 42, 43, 45, 46, 48, 50, 52, 54,
        55, 56, 57, 59, 60, 61, 62, 64, 65, 66, 68, 70, 71, 73, 74, 75, 76,
        77, 78, 79,  0,  1,  2,  3,  4,  5,  6,  7,  9, 11, 12, 14, 15, 17,
        19, 21, 23, 24, 25, 26, 28, 29, 30, 31, 33, 34, 35, 37, 39, 40, 42,
        43, 44, 45, 46, 47, 48, 49]))

And then filter it by:

In [21]: a[np.where(a !=' ')]
Out[21]: 
array(['I', 'a', 'm', 'g', 'o', 'o', 'd', 'I', 'a', 'm', 'g', 'o', 'o',
       'd', 'I', 'a', 'm', 'g', 'o', 'o', 'd', 'I', 'a', 'm', 'g', 'o',
       'o', 'd', 'I', 'a', 'm', 'g', 'o', 'o', 'd', 'I', 'a', 'm', 'g',
       'o', 'o', 'd', 'I', 'a', 'm', 'g', 'o', 'o', 'd', 'I', 'a', 'm',
       'g', 'o', 'o', 'd', 'I', 'a', 'm', 'g', 'o', 'o', 'd', 'I', 'a',
       'm', 'g', 'o', 'o', 'd', 'I', 'a', 'm', 'g', 'o', 'o', 'd', 'I',
       'a', 'm', 'g', 'o', 'o', 'd', 'I', 'a', 'm', 'g', 'o', 'o', 'd',
       'I', 'a', 'm', 'g', 'o', 'o', 'd', 'n', 'e', 'w', '_', 'i', 't',
       'e', 'm', 's', '=', '[', 'x', 'i', 'f', 'x', '%', '2', 'e', 'l',
       's', 'e', 'N', 'o', 'n', 'e', 'f', 'o', 'r', 'x', 'i', 'n', 'i',
       't', 'e', 'm', 's', ']', 'n', 'e', 'w', '_', 'i', 't', 'e', 'm',
       's', '=', '[', 'x', 'i', 'f', 'x', '%', '2', 'e', 'l', 's', 'e',
       'N', 'o', 'n', 'e', 'f', 'o', 'r', 'x', 'i', 'n', 'i', 't', 'e',
       'm', 's', ']', 'n', 'e', 'w', '_', 'i', 't', 'e', 'm', 's', '=',
       '[', 'x', 'i', 'f', 'x', '%', '2', 'e', 'l', 's', 'e', 'N', 'o',
       'n', 'e', 'f', 'o', 'r', 'x', 'i', 'n', 'i', 't', 'e', 'm', 's',
       ']', '\\', 'n'],
      dtype='|S1')

1 Comment

I don't want to remove all white spaces but the whiite spaces from back till a char is found of the numpy list in a multidimensional array

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.