1

Currently, I have data = input('Please input 8 numbers \n') What I want to do is split this list into quarters so I may input them into matrices A and B of a 2x2 format

1
  • Please, how does this realate to numpy? Could you show a desired input/output pair?
    – Ronald Luc
    Commented Mar 8, 2021 at 17:46

3 Answers 3

1

Yeah, for that, you can do

data = input('Please input 8 numbers \n')
quarters = []
split_input = data.split(" ")
for i in range(0, len(split_input), 2):
    quarters.append([split_input[i], split_input[i+1]])

print(quarters)
Please input 8 numbers 
1 2 3 4 5 6 7 8
[['1', '2'], ['3', '4'], ['5', '6'], ['7', '8']]
1

Depending on the order in which the input should be stored in A, B:

import numpy as np

split_input = input('Please input 8 numbers \n').split(" ")
A, B = np.array(split_input, dtype='float').reshape(2, 2, 2)

print(A)
print(B)
Please input 8 numbers 
1 2 3 4 5 6 7 8
[[1. 2.]
 [3. 4.]]
[[5. 6.]
 [7. 8.]]

or

arr = np.array(split_input, dtype='float').reshape(2, 2, 2)
A, B = np.swapaxes(arr, 0, 1)

print(A)
print(B)
[[1. 2.]
 [5. 6.]]
[[3. 4.]
 [7. 8.]]

or

arr = np.array(split_input, dtype='float').reshape(2, 2, 2)
A, B = np.swapaxes(arr, 1, 2)

print(A)
print(B)
[[1. 3.]
 [2. 4.]]
[[5. 7.]
 [6. 8.]]

etc.

2
  • Thats a lot of effort master Luc, I say you deserve an upvote too. Commented Mar 8, 2021 at 18:03
  • Thanks, but that is not, how this site should work 😅. I would rather get a stricter problem description.
    – Ronald Luc
    Commented Mar 8, 2021 at 18:04
0

separated with ,

import numpy as np

_input = input('Please input 8 numbers \n')
int_input = [int(x) for x in _input.split(',')]
x = np.array(int_input).reshape(2, 2, 2)

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.