从python中的用户输入获取二维数组模式 [英] Getting 2-d array pattern from user inputs in python
问题描述
我想给用户输入模式作为input1: 00,01,10,11
和另一个输入作为input2:0.1,0.2,0.24,0.5
这些输入我从这两个输入中一一给出替代值.为了提供用户输入,我正在使用:input = int(raw_input())
.但我想要的输出应该在单独的二维数组中作为 [[0,0],[0,1],[1,0],[1,1]]
和 [[0.1],[0.2],[0.24],[0.5]]
请给我一个好主意.
I want to give the user input pattern as
input1: 00,01,10,11
and another input as
input2: 0.1,0.2,0.24,0.5
and these inputs I am giving alternate value as one by one from these two inputs. For giving user input I am using:
input = int(raw_input())
.
But my desired output should be in separate 2-d array as [[0,0],[0,1],[1,0],[1,1]]
and [[0.1],[0.2],[0.24],[0.5]]
please give me good idea for this.
推荐答案
您可以使用 str.split
将输入的字符串转换为子字符串列表:
You can use str.split
to convert the string input into a list of substrings:
"0.1,0.2".split(",") == ["0.1", "0.2"]
和 map
应用一些对这些子串的处理规则;在您的第二种情况下,这是转换为 float
:
map(float, "0.1,0.2".split(",")) == [0.1, 0.2]
您的第一个示例(二进制字符串到整数列表)将需要 map
的自定义函数,例如:
Your first example (binary strings to lists of integers) will require a custom function for map
, for example:
map(lambda s: map(int, s), "001,010".split(",")) == [[0, 0, 1], [0, 1, 0]]
请注意,在 Python 3.x 中 map
是一个迭代器,因此您可能需要将其显式转换为 list
.
Note that in Python 3.x map
is an iterator, so you may need to explicitly convert it to a list
.
这篇关于从python中的用户输入获取二维数组模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!