python和表格_如何在python中显示表格中每一行和每一列的总和

importrandom

dash="-"*62print("Random Integer Table:")print(dash)print("{:<10s} {:>45s}".format("RANDOM INTEGERS","ROW TOTAL"))print(dash)# Initialize a list that will have the sum of each columnsum_col=[0for_inrange(10)]forrowsinrange(10):random_list=[random.randrange(1,251,1)forrowsinrange(10)]# Save the current sum of each columnssum_col=[x+sum_partforx,sum_partinzip(random_list,sum_col)]stringList=[str(i)foriinrandom_list]row_total=sum(random_list)print("{:<3} {:>4} {:>4} {:>4} {:>4} {:>4} {:>4} {:>4} {:>4}".format(stringList[0],stringList[1],stringList[2],stringList[3],stringList[4],stringList[5],stringList[6],stringList[7],stringList[8],stringList[9]),"{:17d}".format(row_total))print(dash)print("COLUMN TOTAL")print(dash)print("{:<3} {:>4} {:>4} {:>4} {:>4} {:>4} {:>4} {:>4} {:>4}".format(sum_col[0],sum_col[1],sum_col[2],sum_col[3],sum_col[4],sum_col[5],sum_col[6],sum_col[7],sum_col[8],sum_col[9]),"{:17d}".format(sum(sum_col)))# Random Integer Table:# --------------------------------------------------------------# RANDOM INTEGERS ROW TOTAL# --------------------------------------------------------------# 147 54 61 56 183 172 228 110 241 1260# 211 77 80 148 237 98 30 99 8 1016# 216 241 200 31 218 122 183 34 118 1420# 34 147 195 115 61 69 208 155 96 1203# 174 27 39 40 99 241 11 92 114 838# 102 131 8 130 22 225 80 152 159 1032# 129 30 122 87 142 245 202 97 40 1280# 219 226 147 153 217 242 154 129 39 1585# 186 37 123 168 12 152 105 225 57 1072# 120 243 146 39 216 50 177 208 36 1434# --------------------------------------------------------------# COLUMN TOTAL# --------------------------------------------------------------# 1538 1213 1121 967 1407 1616 1378 1301 908 12140

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值