- python - 创建BMI表

这是我的代码:


header = 't{}'.format('t'.join(map(str, range(100, 260, 10))))


rows = []


for i in range(58, 78, 2):


 row = 't'.join(map(str, (bmi for q in range(1, 17))))


 rows.append('{}t{}'.format(i, row))


 print(header + 'n' + 'n'.join(rows))



这是输出:


 100 110 120 130 140 150 160 170 180 190 200 210 220 230 240 250


58 


60 


62 


64 


66 


68 


70 


72 


74 


76 



时间:


In [16]: weights = range(100, 260, 10)


 ...: header = 't' + 't'.join(map(str, weights))


 ...: rows = [header]


 ...: for height in range(58, 78, 2):


 ...: row = 't'.join(f'{weight/height**2*703:.1f}' for weight in weights)


 ...: rows.append(f'{height}t{row}')


 ...: print('n'.join(rows))


 ...:


 100 110 120 130 140 150 160 170 180 190 200 210 220 230 240 250


58 20.9 23.0 25.1 27.2 29.3 31.3 33.4 35.5 37.6 39.7 41.8 43.9 46.0 48.1 50.2 52.2


60 19.5 21.5 23.4 25.4 27.3 29.3 31.2 33.2 35.1 37.1 39.1 41.0 43.0 44.9 46.9 48.8


62 18.3 20.1 21.9 23.8 25.6 27.4 29.3 31.1 32.9 34.7 36.6 38.4 40.2 42.1 43.9 45.7


64 17.2 18.9 20.6 22.3 24.0 25.7 27.5 29.2 30.9 32.6 34.3 36.0 37.8 39.5 41.2 42.9


66 16.1 17.8 19.4 21.0 22.6 24.2 25.8 27.4 29.0 30.7 32.3 33.9 35.5 37.1 38.7 40.3


68 15.2 16.7 18.2 19.8 21.3 22.8 24.3 25.8 27.4 28.9 30.4 31.9 33.4 35.0 36.5 38.0


70 14.3 15.8 17.2 18.7 20.1 21.5 23.0 24.4 25.8 27.3 28.7 30.1 31.6 33.0 34.4 35.9


72 13.6 14.9 16.3 17.6 19.0 20.3 21.7 23.1 24.4 25.8 27.1 28.5 29.8 31.2 32.5 33.9


74 12.8 14.1 15.4 16.7 18.0 19.3 20.5 21.8 23.1 24.4 25.7 27.0 28.2 29.5 30.8 32.1


76 12.2 13.4 14.6 15.8 17.0 18.3 19.5 20.7 21.9 23.1 24.3 25.6 26.8 28.0 29.2 30.4



for q in range(1, 17)你可以把它换成for q in range(100, 260, 10)

...