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
3 Answers
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']]
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.
-
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. Commented Mar 8, 2021 at 18:04
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)
input
/output
pair?