0

I am trying to manipulate an array and perform operations. My input is an array

a= [['f'  '0'  '1'  '0' '1' '0']
    ['o'  '0'  '0'  '1' '0' '0'] 
    ['o'  '0'  '1'  '0' '1' '1']
    ['!b' '1'  '0'  '1' '0' '0']
    ['a'  '0'  '0'  '1' '0' '0'] 
    ['r'  '0'  '1'  '0' '1' '1']]

If I take the first row, my output should just be the columns in which 1 is present. Similarly, I do for each row and get output. So my output should be an array.

output = [['f' '1' '1' 
           'o' '0' '0'  
           'o' '1' '1' 
          '!b' '0' '0' 
           'a' '0' '0'  
           'r' '1' '1' ]
          ['f' '0' 
           'o' '1'  
           'o' '0' 
          '!b' '1' 
           'a' '1'   
           'r' '0' ]
          ['f' '1' '1' '0'
           'o' '0' '0' '0'
           'o' '1' '1' '1'
          '!b' '0' '0' '0'
           'a' '0' '0' '0'
           'r' '1' '1' '1']
          ['f' '0' '0' 
           'o' '0' '1'  
           'o' '0' '0' 
          '!b' '1' '1' 
           'a' '0' '1'  
           'r' '0' '0' ]
          ['f' '0' 
           'o' '1'  
           'o' '0'  
          '!b' '1'  
           'a' '1'  
           'r' '0' ]
          ['f' '1' '1' '0'
           'o' '0' '0' '0'
           'o' '1' '1' '1'
          '!b' '0' '0' '0'
           'a' '0' '0' '0'
           'r' '1' '1' '1']]

Here's my Code

          output = [] 
           for i in a:
               for j in i:
                   if j == 1:
                      output = a[0:]
                      output.append([n][j]) for n in len(i)
                    else:
                         pass
6
  • You'll need to give more information about how the output is derived from the input. Commented Nov 23, 2015 at 9:13
  • Actually it all there. For each row, produce a matrix that has only the columns that have a 1 in this row. Commented Nov 23, 2015 at 9:17
  • I understand your question but you need to show what you have tried. Commented Nov 23, 2015 at 9:22
  • @AkshayHazari sorry, I didnt give the code cause I am not well acquainted with array manipulation Commented Nov 23, 2015 at 10:06
  • @Kristy It is fine about not being acquainted but still some amount of effort needs to be put before asking. Even if you didn't ask, I am not the person who down voted. Commented Nov 23, 2015 at 10:11

1 Answer 1

1

For each row, produce a matrix that has only the columns that have a '1' in this row.

import numpy as np

a = np.array([['f', '0', '1',  '0', '1', '0'],
     ['o', '0', '0',  '1', '0', '0'], 
     ['o', '0', '1',  '0', '1', '1'],
    ['!b', '1', '0',  '1', '0', '0'],
     ['a', '0', '0',  '1', '0', '0'], 
     ['r', '0', '1',  '0', '1', '1']])

l = []
for r in a:
   l.append(a[:, [i for i, c in enumerate(r) if i == 0 or c == '1']])
print l

This works but perhaps someone more familiar with numpy might be able to do better.

Produces:

[array([['f', '1', '1'],
       ['o', '0', '0'],
       ['o', '1', '1'],
       ['!b', '0', '0'],
       ['a', '0', '0'],
       ['r', '1', '1']], 
      dtype='|S2'), array([['f', '0'],
       ['o', '1'],
       ['o', '0'],
       ['!b', '1'],
       ['a', '1'],
       ['r', '0']], 
      dtype='|S2'), array([['f', '1', '1', '0'],
       ['o', '0', '0', '0'],
       ['o', '1', '1', '1'],
       ['!b', '0', '0', '0'],
       ['a', '0', '0', '0'],
       ['r', '1', '1', '1']], 
      dtype='|S2'), array([['f', '0', '0'],
       ['o', '0', '1'],
       ['o', '0', '0'],
       ['!b', '1', '1'],
       ['a', '0', '1'],
       ['r', '0', '0']], 
      dtype='|S2'), array([['f', '0'],
       ['o', '1'],
       ['o', '0'],
       ['!b', '1'],
       ['a', '1'],
       ['r', '0']], 
      dtype='|S2'), array([['f', '1', '1', '0'],
       ['o', '0', '0', '0'],
       ['o', '1', '1', '1'],
       ['!b', '0', '0', '0'],
       ['a', '0', '0', '0'],
       ['r', '1', '1', '1']], 
      dtype='|S2')]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.