如何使用Python将float值输入n键盘矩阵中?

发布于 2025-02-13 11:56:52 字数 634 浏览 1 评论 0原文

赦免如果我的问题太基本了,因为我刚刚开始学习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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回忆凄美了谁 2025-02-20 11:56:53

您可以使用(,) 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 = [[(input(),) for i in range(column)] for i in range(rows)]
print("First Matrix is: ")
for n in matrix_a:
   print(n)

output:

Enter the Number of rows : 1
Enter the Number of Columns: 1
Enter the elements of Matrix:
999
First Matrix is: 
[('999',)]

另外,如果您想要float Elements的元素,则可以使用生成器:

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(float(a) for a in input()) for i in range(column)] for i in range(rows)]
print("First Matrix is: ")
for n in matrix_a:
   print(n)

输入行数:1
输入列数:1
输入矩阵的元素:
555
第一个矩阵是:
[(5.0,5.0,5.0)]

You can use (,) Python tuple creator to avoid splitting input elements:

rows = int(input("Enter the Number of rows : "))   
column = int(input("Enter the Number of Columns: "))

print("Enter the elements of Matrix:")
matrix_a = [[(input(),) for i in range(column)] for i in range(rows)]
print("First Matrix is: ")
for n in matrix_a:
   print(n)

Output:

Enter the Number of rows : 1
Enter the Number of Columns: 1
Enter the elements of Matrix:
999
First Matrix is: 
[('999',)]

Also, you can use a generator if you want a tuple of float elements:

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(float(a) for a in 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 : 1
Enter the Number of Columns: 1
Enter the elements of Matrix:
555
First Matrix is:
[(5.0, 5.0, 5.0)]

許願樹丅啲祈禱 2025-02-20 11:56:53

如果您想能够输入浮点值,则需要定义定界符,例如输入值之间的空间。这也使您可以输入多位数的数字,这在原始版本中是不可能的。

...
matrix_a = [[tuple(map(float, input().split(" ")))
             for i in range(column)] for i in range(rows)]

...

Enter the Number of rows : 2
Enter the Number of Columns: 2
Enter the elements of Matrix:
1.2 3.2 1.4
1 2 3
123
1.3 12 1
First Matrix is: 
[(1.2, 3.2, 1.4), (1.0, 2.0, 3.0)]
[(123.0,), (1.3, 12.0, 1.0)]

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.

...
matrix_a = [[tuple(map(float, input().split(" ")))
             for i in range(column)] for i in range(rows)]

...

Enter the Number of rows : 2
Enter the Number of Columns: 2
Enter the elements of Matrix:
1.2 3.2 1.4
1 2 3
123
1.3 12 1
First Matrix is: 
[(1.2, 3.2, 1.4), (1.0, 2.0, 3.0)]
[(123.0,), (1.3, 12.0, 1.0)]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文