【发布时间】:2015-03-19 10:53:37
【问题描述】:
我在下面设计了这段代码,它基本上将用户的行数、列数、最大值和最小值作为输入。
import random
import math
nrows=int(input("enter the number of rows: "))
ncols=int(input("enter the number of columns: "))
lowval=float(input("enter the lowest value: "))
highval=float(input("enter the highest value: "))
def list(nrows, ncols, lowval, highval):
values=[[0, 0, 0, 0],[0, 0, 0, 0],[0, 0, 0, 0]]
for r in range(nrows):
for c in range(ncols):
values[r][c] = random.uniform(lowval,highval+1)
print(values)
list(nrows, ncols, lowval, highval)
现在我正在努力解决的领域是尝试获取列表并将其转换为类似于图表的更有条理的东西,以便输出基本上反映了这一点,例如:
Number of rows: 4
Number of columns: 3
Low value: -100
High value: 100
0 1 2
0 7.715 6.522 23.359
1 79.955 -4.858 -71.112
2 49.249 -17.001 22.338
3 98.593 -28.473 -92.926
关于如何让我的输出看起来像上面想要的那样有任何建议/想法吗?
编辑:我知道有一种方法可以通过使用 pandas 来做到这一点,但我更感兴趣的是想办法手动完成。
我在下面的这段代码有点接近我想要的输出:
for i in range(ncols):
for a in range(nrows):
print('%-12i%-12i' % (random.uniform(lowval,highval+1), random.uniform(lowval,highval+1)))
这基本上给了我一个输出:
enter the number of rows: 2
enter the number of columns: 3
enter the lowest value: 10
enter the highest value: 100
94 36
95 33
20 79
17 19
57 63
60 30
我现在需要做的基本上是让我的行和列与用户输入的匹配,如果有办法请告诉我!
【问题讨论】: