Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-检查元素是否在列表中存在问题_Python_List_Function_For Loop_If Statement - Fatal编程技术网

Python-检查元素是否在列表中存在问题

Python-检查元素是否在列表中存在问题,python,list,function,for-loop,if-statement,Python,List,Function,For Loop,If Statement,我在Python中遇到了一个问题,因为我这样做是为了好玩,而不是作为一名专业人员,我不明白为什么这不起作用。我有一个包含其他列表的列表,每个列表中有两个数字。此函数应检查元素+1和元素-1是否为我的tp列表中的元素。如果是,则将它们附加到堆栈中。代码: def check(): tp = [[0, 1], [1, 1], [1, 2], [2, 2], [2, 3], [3, 3]] stack = [] for i in tp: a = i[0]

我在Python中遇到了一个问题,因为我这样做是为了好玩,而不是作为一名专业人员,我不明白为什么这不起作用。我有一个包含其他列表的列表,每个列表中有两个数字。此函数应检查元素+1和元素-1是否为我的tp列表中的元素。如果是,则将它们附加到堆栈中。代码:

def check():
    tp = [[0, 1], [1, 1], [1, 2], [2, 2], [2, 3], [3, 3]]
    stack = []
    for i in tp:
        a = i[0]
        b = i[1]
        if [(a - 1), (b - 1)] and [(a + 1), (b + 1)] in tp:
            stack.append(i)
    return stack
不幸的是,结果是:

[[0, 1], [1, 1], [1, 2], [2, 2]]
[1,2]
是正确的,因为
[0,1]
[2,3]
tp
的元素

[2,2]
是正确的,因为
[1,1]
[3,3]
tp
的元素

为什么这个函数也给了我另外两个呢? 例如:tp的第一个元素是[0,1]->[-1,0],并且[1,2]应该是计算的输出,但显然[-1,0]不在该列表中。我的(可能是明显的)错误在哪里?提前感谢。

如果tp中有[(a-1)、(b-1)]和[(a+1)、(b+1)]的话
如果tp中的[(a-1),(b-1)]和tp中的[(a+1),(b+1)],则应为

def check():
    tp = [[0, 1], [1, 1], [1, 2], [2, 2], [2, 3], [3, 3]]
    stack = []
    for i in tp:
        a = i[0]
        b = i[1]
        if [(a - 1), (b - 1)] in tp and [(a + 1), (b + 1)] in tp:
            stack.append(i)
    return stack

[(a-1),(b-1)]
本身就是一个非空列表,它被认为是真实的,使您的if
if True和tp中的[(a+1),(b+1)]
不会分布在
中。您的条件被解析为

if [(a - 1), (b - 1)] and ([(a + 1), (b + 1)] in tp):
def check():
    tp = [[0, 1], [1, 1], [1, 2], [2, 2], [2, 3], [3, 3]]
    stack = []
    for i in tp:
        a = i[0]
        b = i[1]
        if [(a - 1), (b - 1)] in tp and [(a + 1), (b + 1)] in tp:
            stack.append(i)
    return stack
因为
[(a-1),(b-1)]
是真实的,所以条件总是成功的

对于要检查的每个列表,您需要在
中使用

if [(a - 1), (b - 1)] in tp and [(a + 1), (b + 1)] in tp:

现在,您正在检查两件事:

1:
[(a-1)、(b-1)]
不是空的(在您的示例中总是
True

tp中的2:
[(a+1)、(b+1)]

def check():
    tp = [[0, 1], [1, 1], [1, 2], [2, 2], [2, 3], [3, 3]]
    stack = []
    for i in tp:
        a = i[0]
        b = i[1]
        if [(a - 1), (b - 1)] in tp and [(a + 1), (b + 1)] in tp:
            stack.append(i)
    return stack
您要检查的是:
([(a-1),(b-1)]在tp中)和([(a+1),(b+1)]在tp中)


您的条件语句不正确

应该是:

if [(a - 1), (b - 1)] in tp and [(a + 1), (b + 1)] in tp:
而不是:

if [(a - 1), (b - 1)] and [(a + 1), (b + 1)] in tp:
使用tp中的
if[(a-1)、(b-1)]和[(a+1)、(b+1)],如果:

  • [(a-1)、(b-1)]
    表示列表不是空的
  • [(a+1)、(b+1)]在tp中满足

  • 因为条件
    1
    总是
    True
    ,它归结为第二个条件,也满足
    [0,1]
    [1,1]
    ,所以你也可以在结果中得到它。

    如果在tp中[(a-1)、(b-1)]和[(a+1)、(b+1)]在tp:
    中,由于您在第一个检查器中求值
    True
    如果[(a-1)、(b-1)]
    =列表中的
    True
    不是空的,您必须在
    tp
    中检查您的第一个检查器,因此
    True
    求值得到元素的
    True
    。。