Python|列表的逆向遍历

1···list1 = [ 'apple', 'banana', 'grape', 'peach', 'orange', 'pear' ],按照样例输出格式逆向将列表中的元素逐一遍历输出

list1 = [ 'apple', 'banana', 'grape', 'peach', 'orange', 'pear' ]
for i in range(len(list1)-1,-1,-1):
    print(list1[i],end=",")

 

2···列表如下:list1 = [ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 20] ],按3行4列形式打印输出如下的二维列表(每个元素靠左对齐,占3列 

list1 = [ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 20] ]

for i in range(len(list1)):
    for j in range(len(list1[0])):
        print("{:<3d}".format(list1[i][j]),end="")
    print()

 

3···a=[3,6,9],键盘输入列表b,计算a中元素和b中对应元素乘积的累加和。(要求列表b为[1,2,3],累加和是:3*1+6*2+9*3=42,屏幕输出计算结果42即可) 

a  =[3,6,9]
b = eval(input())
s=0
for i in range(3):
    s=s+a[i]*b[i]
print(s)

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值