如何使用Python将float值输入n键盘矩阵中?
赦免如果我的问题太基本了,因为我刚刚开始学习python
rows = int(input("Enter the Number of rows : "))
column = int(input("Enter the Number of Columns: "))
print("Enter the elements of Matrix:")
matrix_a = [[tuple(input()) for i in range(column)] for i in range(rows)]
print("First Matrix is: ")
for n in matrix_a:
print(n)
获取输出,因为
Enter the Number of rows : 2 Enter the Number of Columns: 2 Enter the elements of Matrix: 123 456 987 644 First Matrix is: [('1', '2', '3'), ('4', '5', '6')] [('9', '8', '7'), ('6', '4', '4')]
我如何将float值作为此n-tuple矩阵的元素输入。
如[(0.9,0.6,0.5),(0.4,0.5,0.1)]
Pardon if my question is too basic since I've just started learning python
rows = int(input("Enter the Number of rows : "))
column = int(input("Enter the Number of Columns: "))
print("Enter the elements of Matrix:")
matrix_a = [[tuple(input()) for i in range(column)] for i in range(rows)]
print("First Matrix is: ")
for n in matrix_a:
print(n)
Getting the output as
Enter the Number of rows : 2 Enter the Number of Columns: 2 Enter the elements of Matrix: 123 456 987 644 First Matrix is: [('1', '2', '3'), ('4', '5', '6')] [('9', '8', '7'), ('6', '4', '4')]
How can I input float values as elements of this n-tuple matrix.
like [ (0.9, 0.6, 0.5), (0.4, 0.5, 0.1) ]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
(,)
python元组创建者来避免分裂输入元素:output:
另外,如果您想要float Elements的元素,则可以使用生成器:
You can use
(,)
Python tuple creator to avoid splitting input elements:Output:
Also, you can use a generator if you want a tuple of float elements:
如果您想能够输入浮点值,则需要定义定界符,例如输入值之间的空间。这也使您可以输入多位数的数字,这在原始版本中是不可能的。
If you want to be able to input float values, you need to define a delimiter, e.g. a space between the input values. This also enables you to enter multi-digit numbers, which is not possible in the original version.