python 列表迭代_Python | 以相反的顺序迭代列表

python 列表迭代

Given a list and we have to iterate it in reverse order in python.

给定一个列表,我们必须在python中以相反的顺序对其进行迭代。

Example:

例:

    Input:
    List = [10, 20, 30, 40, 50]
    Output:
    list = [50, 40, 30, 20, 10]


    Input;
    list = ['Hello', 10 'World', 20]
    Output:
    list = [20, 'World', 10, 'Hello']

以相反的顺序迭代列表 (Iterate a list in reverse order)

To iterate a list in reverse order, list[::-1] is used. list[::-1] will return the list in reverse order.

要以相反的顺序迭代列表,请使用list [::-1] 。 list [::-1]将以相反的顺序返回列表。

Program:

程序:

# define a list
list1  = [10, 20, 30, 40, 50]

# print the list 
print "original list: ", list1

# iterate the list
list1 = list1[::-1]

# print the list 
print "list in reverse order: ", list1

# another list with string and integer elements
list2 = ['Hello', 10, 'world', 20]

# print the list
print "Original list: ", list2

# iterate the list
list2 = list2[::-1]

# print the list
print "list in reverse order: ", list2

Output

输出量

    original list:  [10, 20, 30, 40, 50]
    list in reverse order:  [50, 40, 30, 20, 10]
    Original list:  ['Hello', 10, 'world', 20]
    list in reverse order:  [20, 'world', 10, 'Hello']


翻译自: https://www.includehelp.com/python/iterate-a-list-in-reverse-order.aspx

python 列表迭代

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值