简体   繁体   English

如何将用户输入的 8 个数字列表拆分为四等分

[英]How to split a user inputted list of 8 numbers into quarters

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目前,我有data = input('Please input 8 numbers \n')我想要做的是将此列表分成四等份,以便我可以将它们输入到 2x2 格式的矩阵 A 和 B

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:根据输入应存储在 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.等等

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM