当前位置:  开发笔记 > 编程语言 > 正文

如何检查一个数字是否在列表列表中?

如何解决《如何检查一个数字是否在列表列表中?》经验,为你挑选了1个好方法。

所以我一直无法检查数字0是否在另一个列表中的一组列表中.这些行构成了我正在制作的pacman类型游戏的迷宫,所以这一点就是检查pacman是否已经吃掉了所有的硬币.

这是我的代码:

row1 =[3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3]
row2 =[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
row3 =[1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1]
row4 =[1,0,1,1,0,1,1,1,0,1,0,1,1,1,0,1,1,0,1]
row5 =[1,0,1,1,0,1,1,1,0,1,0,1,1,1,0,1,1,0,1]
row6 =[1,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,1]
row7 =[1,0,1,1,0,1,0,1,1,1,1,1,0,1,0,1,1,0,1]
row8 =[1,0,0,0,0,1,0,1,1,1,1,1,0,1,0,0,0,0,1]
row9 =[1,1,1,1,0,1,0,0,0,1,0,0,0,1,0,1,1,1,1]
row10=[3,3,3,1,0,1,1,1,0,1,0,1,1,1,0,1,3,3,3]
row11=[3,3,3,1,0,1,0,0,0,0,0,0,0,1,0,1,3,3,3]
row12=[3,3,3,1,0,1,0,1,4,4,4,1,0,1,0,1,3,3,3]
row13=[1,1,1,1,0,1,0,1,3,3,3,1,0,1,0,1,1,1,1]
row14=[3,3,3,3,0,0,0,1,3,3,3,1,0,0,0,3,3,3,3]
row15=[1,1,1,1,0,1,0,1,5,5,5,1,0,1,0,1,1,1,1]
row16=[3,3,3,1,0,1,0,3,3,3,3,3,0,1,0,1,3,3,3]
row17=[3,3,3,1,0,1,0,1,1,1,1,1,0,1,0,1,3,3,3]
row18=[3,3,3,1,0,1,0,1,1,1,1,1,0,1,0,1,3,3,3]
row19=[1,1,1,1,0,0,0,0,0,1,0,0,0,0,0,1,1,1,1]
row20=[1,0,0,0,0,1,1,1,0,1,0,1,1,1,0,0,0,0,1]
row21=[1,0,1,1,0,0,0,0,0,3,0,0,0,0,0,1,1,0,1]
row22=[1,0,1,1,0,1,0,1,1,1,1,1,0,1,0,1,1,0,1]
row23=[1,2,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,2,1]
row24=[1,0,1,1,1,1,1,1,0,1,0,1,1,1,1,1,1,0,1]
row25=[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1]
row26=[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]

maze = [row1,row2,row3,row4,row5,row6,row7,row8,row9,row10,row11,row12,
        row13,row14,row15,row16,row17,row18,row19,row20,row21,row22,
        row23,row24,row25,row26]

所以我试过if 0 not in maze:但是if语句将执行0是否在(迷宫)中.

任何帮助将不胜感激,谢谢.



1> TerryA..:

你必须遍历每个列表maze并检查是否有0:

if not any(0 in i for i in maze):
    ...

any()函数的优点在于它maze一旦找到0 就会停止循环.

推荐阅读
coco2冰冰
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有