比较两个二维列表 [英] Comparing two 2-Dimensional lists

查看:147
本文介绍了比较两个二维列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何比较两个2-dim列表的位置.例如,我有两个带有0和1的2-dim列表,并且我想创建一个函数,如果两个列表中的1位于同一位置,则该函数将返回True.

I was wondering how I would compare two 2-dim lists by their location. For instance, I have two 2-dim lists with 0's and 1's ,and I want to create a function that would return True if a 1 is in the same location in both lists.

List1=[[0,0,0],
       [1,0,0],
       [0,0,1]]

List1=[[0,0,0],
       [1,0,0],
       [0,0,0]]

这是我开始的目的,但是我不确定如何继续.

Here is what I started with, but I am not sure how to continue.

def collide(a,b):
    for x in range(3):
        for y in range(3):
collide(List1, List2)

有任何提示吗?我在想

if a[x][y]==1 and b[x][y]==1:
    return True

但这可能是不正确的.

推荐答案

对我来说,这很好,尽管您并没有考虑两个条目也为0.

That looks fine to me, although you don't account for the two entries being 0 as well.

您可能还想使用列表的实际长度.因此,

You might also want to use the actual length of the lists. So instead

for x in range(len(a)):
    for y in range(len(a[x])):
        ...

假定列表的大小相同.

确保在函数末尾也返回false(因为如果发生冲突,它不会到达该行)

Make sure that you also return false at the end of the function (since it wouldn't have gotten to that line if there were a collision)

这篇关于比较两个二维列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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