简体   繁体   English

如何获得仅打印一个输出的代码?

[英]How do I get my code to only print one output?

Essentially, I am trying to make a function which reads an array, and a number. 本质上,我正在尝试创建一个读取数组和数字的函数。 If the number is within the array, it should return True , and False otherwise. 如果数字在数组中,则应返回True ,否则返回False However, I find that for each element in the array there is a True or False - the code checks everything individually, when I only need one True or False ; 但是,我发现对于数组中的每个元素都有一个TrueFalse当我只需要一个TrueFalse时,代码会单独检查所有内容; is the number in the array or not ? 是数组中的数字吗?

def is_it_there(arr, k):
    for x in arr:
        if x == k:
            print(True)
        else:
            print(False)

is_it_there([8,5,2,234,426,7,11],11)

Like I said before, only one True was expected, but each item was checked and so it was False, False, False, False, False, False, True 就像我之前说过的,只期望一个True,但是检查了每个项目,所以它是False, False, False, False, False, False, True

It's just 只是

if k in arr:
   print(True)
else:
   print(False)

or, simpler 或者,更简单

print(k in arr)

You can refactor your code like this: 您可以这样重构代码:

def is_it_there(arr, k):
    for x in arr:
        if x == k
            return True
    return False

print(is_it_there([8,5,2,234,426,7,11],11))

If you find the item anywhere in the list, print True and exit the function immediately. 如果您在列表中的任何位置找到该项目,请打印True并立即退出功能。

If you make it all the way to the end of the list without finding it, only then print False . 如果一直到列表末尾都没有找到它,则仅打印False

def is_it_there(arr, k):
    for x in arr:
        if x == k:
            # print True and terminate the function
            print(True)
            return
    # if we made it all the way through the loop, we didn't find it
    print(False)

However, the 'in' operator already does what you want: 但是,“ in”运算符已经可以满足您的要求:

if value in mylist:
    print 'True'
else:
    print 'False'

The problem rises from the fact because you are checking it constantly while looping through the elements. 问题源于事实,因为您在循环遍历元素时不断对其进行检查。 So the code is going to check through constantly whether or not the element is present or not. 因此,代码将不断检查该元素是否存在。 So you are going to end up printing a statement everytime. 因此,您将最终每次都打印一条语句。 Python gives a neat way to do achieve this Python提供了一种完美的方法来实现这一目标

def is_it_there(arr, k):
    if k in arr:
       print(True)
    else:
       print(False)

is_it_there([8,5,2,234,426,7,11],11)

You are printing a result each time you do a test in your loop. 每次在循环中进行测试时,您都在打印结果。 Try to record the result of each round of your test in a list, and test if there is any "True" value in your result list. 尝试将每个测试回合的结果记录在一个列表中,并测试结果列表中是否有任何“ True”值。

Code Example: 代码示例:

def is_it_there(arr, k):
    result = list()
    for x in arr:
        if x == k:
            result.append(True)
        else:
            result.append(False)
    if True in  result:
        print(True)
    else:
        print(False)
is_it_there([8,5,2,234,426,7,11],11)

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

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