简体   繁体   English

Python列表检查

[英]Python list checking

So on a list I need to check if 'z' is in the same row as the case selected and return True, but if something is on the way for example 'x' it returns False. 因此,在列表上,我需要检查“ z”是否与所选大小写在同一行中,并返回True,但是如果途中出现某些情况(例如“ x”),则返回False。 I made a code but the problem it has is that if there is two 'z's and the one on the left is blocked by an 'x' but the other one is not, it returns False but it should be True. 我编写了一个代码,但问题是,如果有两个'z',而左边的一个被'x'阻止,而另一个则没有,则返回False,但它应该为True。 It works fine for the other cases. 在其他情况下也可以正常工作。 Hope you get me, I'm pretty bad explaining these things. 希望你能得到我,我很难解释这些事情。

def row(lst,i,j):
    c = 0
    if 'z' not in lst[i]:
        c += 1
    else:
        for n in range(0,len(lst[i])):
            if lst[i][n] == 'z' and n > j:
                for m in range(j,n):
                    if lst[i][m] == 'x':
                        c += 1
                    else:
                        c = 0
                break

            if lst[i][n] == 'z' and j > n:
                for m in range(n,j):
                    if lst[i][m] == 'x':
                        c += 1
                    else:
                        c = 0
                break

    if c > 0:
        return False
    elif c == 0:
        return True

I don't really know what exactly does 'break' here but it works The list I use is: 我真的不知道“中断”在这里到底是什么,但是它起作用了。我使用的列表是:

lst = [['_','_','_','_','_'],['z','x','_','_','z'],['_','_','x','_','_']]

and the case I'm checking is lst[1][2]. 我正在检查的情况是lst [1] [2]。

['_','x','_','_','z']

returns True, there is a 'z' in the row. 返回True,该行中有一个“ z”。

['_','x','_','x','z']

returns False, z is being blocked 返回False,z被阻止

['z','x','_','x','z']

returns False 返回False

['z','x','_','_','z']

PROBLEM: should return True 问题:应返回True

There are two halves of the row to check. 该行有两半要检查。 The one before (i, j) and the one after (i, j). (i,j)之前的一个和(i,j)之后的一个。

The problem is that you're breaking before you reach the second half. 问题在于您在下半场之前就已经破产了。 Plus, I'm not sure what you're doing with c . 另外,我不确定您对c的处理方式

Anyway, one solution could be this: 无论如何,一种解决方案可能是这样的:

def check_row(lst, i, j):
    if 'z' not in lst[i]:
        return False

    for n in range(j, 0, -1):
        if lst[i][n] == 'z':
            return True
        elif lst[i][n] == 'x':
            break

    for n in range(j + 1, len(lst[i])):
        if lst[i][n] == 'z':
            return True
        elif lst[i][n] == 'x':
            break

    return False

The idea is to look out from the target value. 这个想法是要从目标值中看出来 First, we loop from the target to the front of the row, and then we loop from the target to the end of the row. 首先,我们从目标循环到行的开头,然后从目标循环到行的结尾。 At any point, if it encounters 'z', we return True immediately. 在任何时候,如果遇到“ z”,我们都会立即返回True。 If it encounters 'x', we break the search in that direction. 如果遇到“ x”,我们将朝该方向中断搜索。 We have been blocked. 我们被封锁了。

If we reach the end, it means we haven't found 'z', so we return False. 如果到达末尾,则表示尚未找到“ z”,因此返回False。

This could probably be made much shorter but this basic version should work. 可以将其缩短一些,但是此基本版本应该可以使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM