- Please enter the row and column [m, n] of the matrix, and randomly generate a matrix of mxn size (here it is required to store in the form of a list). Please realize the transposition of the matrix. (Code+result screenshot)
原题如上:
1.
# 第二题 import random m=int(input("please input the row")) n=int(input ("please input the column")) x=m*n d=list() d=[random.randint(1,100) for _ in range(x)] i=0 for d in n: print(d[i]) i+=1
报错:for d in n:
TypeError: 'int' object is not iterable
解决:是for循环的迭代问题
问题:
如何将随机数输出在同一行;
解决:
学会了用enumerate函数同时返回列表中数的索引和值,在print中修改end就可以在同一行中输出,
问题:为什么没有对齐
解决:破案了,数列第一项索引是0;
爽了。