3

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?

1
  • You should probably move to numpy ... Commented Apr 16, 2013 at 4:05

5 Answers 5

3

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
Sign up to request clarification or add additional context in comments.

2 Comments

Are you sure that last line is correct? On my version of NumPy (1.5.1), repeat needs two arguments. Should that also be arange?
@Blair, indeed it should have been arange, I've fixed it.
2

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]

Comments

1

try this:

from itertools import chain, count, tee

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

Comments

0

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*']]

Comments

0

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)]

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

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.