saifuddin778
saifuddin778

Reputation: 7277

Select specific elements from an array

I have an array:

  X = [[5*,  0,     0,      0,      0,      0,      0,      0],
       [9*,  6,     0,      0,      0,      0,      0,      0],
       [4,   6*,    8,      0,      0,      0,      0,      0],
       [0,   7*,    1,      5,      0,      0,      0,      0],
       [9,   3,     3*,     4,      4,      0,      0,      0],
       [4,   5,     5*,     6,      7,      5,      0,      0],
       [4,   5,     6,      8*,     7,      7,      8,      0], 
       [4,   7,     8,      9*,     7,      3,      9,      6]]

I want to select and append all the values that are marked by *. The approach is basically to select 0th element from 0th and 1st row...1th element from 2nd and 3rd row..and so on.

The resulting set should be:

  Result = ((X[0][0], (X[1][0]), (X[2][1], X[3][1]), (X[4][2], X[5][2]), (X[6][3], X[7][3]))

Which can be written as:

  Result = ((X[n+0][n], (X[n+1][n]), (X[n+2][n+1], X[n+3][n+1]), (X[n+4][n+2], X[n+5][n+2]), (X[n+6][n+3], X[n+7][n+3]))

  Where n = 0

How do I do that? I have applied this but its not working:

 Result = []

 for a in X:
     Result.append([[[ a[i][j] ] for i in range(0,8)] for j in range(0,8)])

But no results. Any guesses?

Upvotes: 3

Views: 2652

Answers (5)

I think the simpliest way in one line:

Result = [[X[index-1][int(index/2-0.5)],X[index][int(index/2-0.5)]] for index in range(1,len(X),2)]

Upvotes: 0

Bi Rico
Bi Rico

Reputation: 25813

Because of the numpy tag I thought I would add this:

import numpy as np
X = np.array([[5 ,  0,     0,      0,      0,      0,      0,      0],
              [9 ,  6,     0,      0,      0,      0,      0,      0],
              [4,   6 ,    8,      0,      0,      0,      0,      0],
              [0,   7 ,    1,      5,      0,      0,      0,      0],
              [9,   3,     3 ,     4,      4,      0,      0,      0],
              [4,   5,     5 ,     6,      7,      5,      0,      0],
              [4,   5,     6,      8 ,     7,      7,      8,      0], 
              [4,   7,     8,      9 ,     7,      3,      9,      6]])
i = np.array([0, 1, 2, 3, 4, 5, 6, 7])
j = np.array([0, 0, 1, 1, 2, 2, 3, 3])

result = X[i, j]
print result
# [5 9 6 7 3 5 8 9]

To generate i and j in the general case you can do something like:

n = 8
i = np.arange(n)
j = np.arange(n) // 2

Upvotes: 3

dawg
dawg

Reputation: 103744

In Numpy:

import numpy as np
x = [['5*','0 ','0 ','0 ','0 ','0 ','0 ','0 '],
     ['9*','6 ','0 ','0 ','0 ','0 ','0 ','0 '],
     ['4 ','6*','8 ','0 ','0 ','0 ','0 ','0 '],
     ['0 ','7*','1 ','5 ','0 ','0 ','0 ','0 '],
     ['9 ','3 ','3*','4 ','4 ','0 ','0 ','0 '],
     ['4 ','5 ','5*','6 ','7 ','5 ','0 ','0 '],
     ['4 ','5 ','6 ','8*','7 ','7 ','8 ','0 '],
     ['4 ','7 ','8 ','9*','7 ','3 ','9 ','6 ']]

a=np.array(x)

Then do a list comprehension and/or Numpy slice to get the items:

[a[i:,j][:2].tolist() for i,j in zip(range(0,7,2),range(0,7,1))]

or

[a[i*2:,i][:2].tolist() for i in range(len(a)//2)]

or

a[range(len(a)),np.repeat(range(len(a)//2),2)].reshape(4,2).tolist()

Any case, output is:

[['5*', '9*'], ['6*', '7*'], ['3*', '5*'], ['8*', '9*']]

Upvotes: 0

Volatility
Volatility

Reputation: 32300

This will work if X has an even number of lists in it:

>>> [(X[2*i][i], X[2*i+1][i]) for i in range(len(X)//2)]
[(5, 9), (6, 7), (3, 5), (8, 9)]

If you don't mind flattened lists, then then will work for X of any length:

>>> [lst[idx//2] for idx, lst in enumerate(X)]
[5, 9, 6, 7, 3, 5, 8, 9]

Upvotes: 2

thkang
thkang

Reputation: 11543

try this:

from itertools import chain, count, tee

lst = [row[i] for row, i in zip(array, chain.from_iterable(zip(*tee(count(), 2))))]

Upvotes: 1

Related Questions