如何修复正在打印的多个布尔值

时间:2019-04-15 14:50:54

标签: python-3.x

现在,我被认为是我的函数不正确,因为我得到的布尔输出不止1个。

import static java.util.stream.IntStream.rangeClosed;

class Benchmark {
    public static void main(String[] args) {
        final int iterations = 100_000_000;
        long start = System.currentTimeMillis();
        rangeClosed(1, 50).parallel()
                .forEach(i -> rangeClosed(1, iterations).mapToDouble(Math::sqrt).sum());
        System.out.println(System.currentTimeMillis() - start);
    }
}

1 个答案:

答案 0 :(得分:2)

您不想为每个项目打印 TrueFalse;您想在循环遍历的过程中创建一个布尔值。或者更简单地说,只要找到一个未通过测试的元素,就可以返回False,只有在整个循环中都返回而不返回的情况下,才返回True

def checkStringLength(searchInteger, lstStrings):
    'return Boolean value if the strings are shorter/longer than the first argument'
    for i in lstStrings:
        if len(i) < searchInteger:
            return False
    return True

这自然是使用all函数编写的:

def checkStringLength(searchInteger, lstStrings):
    return all(len(i) >= searchInteger for i in lstStrings)