如何for循环反向? [英] How to for loop in reverse?

查看:223
本文介绍了如何for循环反向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个水模拟程序,我需要它对y,x进行for循环.但是我需要它先检查最底的y,然后再检查.这是我的lvl:

I'm making a water simulation program, and I need it to do a for loop through y, x. But I need it to check the most bottom y first, then up. This is my lvl:

lvl = [[0, 0, 1, 0, 0],
       [0, 0, 1, 0, 0],
       [0, 0, 1, 0, 0],
       [0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0]]

我需要先检查lvl [4],然后再检查lvl [3],lvl [2]等.请帮忙!

I need it to check lvl[4] and then lvl[3], lvl[2], etc. Please help!

NB:我使用嵌套的for循环,因此可以检查y,x.

NB: I'm using nested for loops, so I can check y, x.

推荐答案

您可以使用 reversed 内置方法可以反转列表的顺序:

You can use the reversed built-in method to reverse the ordering of your list of lists:

for li in reversed(lvl):
    print li

输出:

[0, 0, 0, 0, 0]
[0, 0, 0, 0, 0]
[0, 0, 1, 0, 0]
[0, 0, 1, 0, 0]
[0, 0, 1, 0, 0]

这篇关于如何for循环反向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆