小编Ama*_*man的帖子

python程序从用户输入生成n*n矩阵

我想从用户输入矩阵的值n和元素生成一个*n 矩阵.

以下是代码:

n=int(input("Enter the matrix size"))

import numpy as np

#initialise nxn matrix with zeroes
mat=np.zeros((n,n))

#input each row at a time,with each element separated by a space
for i in range(n):
    for j in range(n):
        mat[i][j]=input()
print(mat)  
Run Code Online (Sandbox Code Playgroud)

但我得到这样的输出

[[1. 2.]

 [3. 4.]]
Run Code Online (Sandbox Code Playgroud)

用一个.(点)在我不想要的数字之后.有没有办法通过使用循环和数组来获得这个NumPy

python python-3.x

3
推荐指数
2
解决办法
323
查看次数

如何避免python循环中的最后一个逗号

我想以逗号分隔的形式打印结果输出,但我在最后一个值中得到一个逗号,所以我怎么能删除它.

import math

    d = input().split(',')
    d = [int(i) for i in d]
    c=50
    h=30
    result=[]
    for i in d:
        q=int(round(math.sqrt((2*c*i)/h)))
        result.append(q)
    for i in result:
        print(i, end=",")
Run Code Online (Sandbox Code Playgroud)

这是一个输入的例子我给予和输出我得到

input : 10,20,30,40
output : 6,8,10,12,
Run Code Online (Sandbox Code Playgroud)

我怎么能避免得到最后一个逗号

python python-3.x

0
推荐指数
1
解决办法
635
查看次数

标签 统计

python ×2

python-3.x ×2