Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.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 3.x 如果未指定numpy数组的值,如何返回fail_Python 3.x - Fatal编程技术网

Python 3.x 如果未指定numpy数组的值,如何返回fail

Python 3.x 如果未指定numpy数组的值,如何返回fail,python-3.x,Python 3.x,如果numpy数组包含的值不是0或10,我希望代码返回“fail”语句。检查第2列和第3列,只要有一个值不满足条件,代码就会返回“fail”。我的代码总是返回pass 数据文件的格式为: 0 0 0 0 1 0 0 0 2 0 0 0 3 0 0 0 4 50 10 0 5 10 10 0 6 10 10 0 7 10 10 0 在比较之前使用any,这意味着将bool与int进行比较 data = np.loadtxt('datafile.txt') x1, x2 = data[:

如果numpy数组包含的值不是0或10,我希望代码返回“fail”语句。检查第2列和第3列,只要有一个值不满足条件,代码就会返回“fail”。我的代码总是返回pass

数据文件的格式为:

0 0 0 0
1 0 0 0
2 0 0 0
3 0 0 0
4 50 10 0
5 10 10 0
6 10 10 0    
7 10 10 0


在比较之前使用
any
,这意味着将
bool
int
进行比较

data = np.loadtxt('datafile.txt')
x1, x2 = data[:,1], data[:2]

if np.any(x1 != 0) or np.any(x1 !=10):
    x1label = 'fail'
else:
    x1label = 'pass'

if np.any(x2 !=0) or np.any(x2 !=10):
    x2label = 'fail'
else:
    x2label = 'pass'

if (x1label == 'fail') or (x2label == 'fail'):
    label = 'fail'
else:
    label = 'pass'

一种不同的方法,我发现它是有效的

    import numpy as np

    a_file = open('datafile.txt', 'r')
    LINES = a_file.readlines()
    a_file.close()

    for i in range(len(LINES)):
        line = LINES[i]
        TOKS = line.split()
        x1 = TOKS[1]
        x2 = TOKS[2]
        if (x1 != '0' and x1 != '10'):
            x1label = 'fail'
            break        
        else:
            x1label = 'pass'

        if (x2 != '0' and x2 != '10'):
            x2label = 'fail'
            break        
        else:
            x2label = 'pass'

    if (x1label == 'fail') or (x2label == 'fail'):
            label = 'fail'
    else:
            label = 'pass'

如果我不使用any,它会得到以下错误值error:包含多个元素的数组的真值不明确。使用a.any()或a.all()@仓鼠你可以使用
np.any
,为什么不可以。我的错误。在比较之前,我没有看到你有任何移动。现在返回的是正确的fail。但是当我进入一个循环,读取第二个文件时,应该给我“通过”,它不是。我现在将它放入一个循环,逐个读取多个数据文件,并进行相同的评估。我希望第一个返回失败,第二个返回通过。我不知道如何在注释下编写示例代码。我想我知道为什么循环不起作用。作为测试,我将数据文件中的“50”改为“10”,您的代码应该返回“pass”,但不是。
    import numpy as np

    a_file = open('datafile.txt', 'r')
    LINES = a_file.readlines()
    a_file.close()

    for i in range(len(LINES)):
        line = LINES[i]
        TOKS = line.split()
        x1 = TOKS[1]
        x2 = TOKS[2]
        if (x1 != '0' and x1 != '10'):
            x1label = 'fail'
            break        
        else:
            x1label = 'pass'

        if (x2 != '0' and x2 != '10'):
            x2label = 'fail'
            break        
        else:
            x2label = 'pass'

    if (x1label == 'fail') or (x2label == 'fail'):
            label = 'fail'
    else:
            label = 'pass'