简体   繁体   English

Python检查几个if条件(用数字查找并替换单词)

[英]Python checking several if-conditions (find & replace word with number)

I want to find a string (blue or red) and turn it into a number (1 or 2). 我想找到一个字符串(蓝色或红色)并将其转换为数字(1或2)。 I have in my Code 2 if-Conditions. 我的代码2中有if条件。 Both of them are true. 他们两个都是对的。 So I should get as an output the number 1 and 2. 所以我应该将数字1和2作为输出。
But depending on that where I put return, I always get either 1 or 2, but never 1 and 2 at the same time. 但是,根据我将收益放在何处,我总是得到1或2,但永远不会同时得到1和2。 What am I missing? 我想念什么? Is it not possible to have 2 if-conditions at the same time? 不能同时拥有2个if条件吗?
My Input-Text looks for example like this: 我的输入文本看起来像这样:

myInput = "I like the colors blue and red."

def check_color(document):

    for eachColor in document:

        if ("blue" in myInput):
            color_status = "1"
            #return color_status # only 1 as result

        if ("red" in myInput):
            color_status = "2"
            #return color_status # only 1 as result
        else:
            color_status = "0"
            #return color_status # only 1 as result
    #return color_status # only 2 as result

Without any return --> Output: None 没有任何回报->输出:无

function call 函数调用

color_output = check_color(myInput)
print(color_output)

Of course you need a return statement to get any result at all. 当然,您需要一个return语句才能获得任何结果。 The problem is in the concept: If you want to return zero or more values, a list would be the easiest solution. 问题出在概念上:如果要返回零个或多个值,列表将是最简单的解决方案。 (Your code simply overwrites the 1 by the 2). (您的代码只是将1乘以2)。

   color_status = []
   if "blue" in myInput:
        color_status.append("1")

   if "red" in myInput:
        color_status.append("2")

   return color_status

This is one approach. 这是一种方法。

Demo: 演示:

myInput = "I like the colors blue and red."

def check_color(document):
    clr = ["blue", "red"]
    color_status = 0
    if all(i in document for i in clr):
        color_status = [1, 2]

    elif ("red" in myInput):
        color_status = 2
    elif ("blue" in myInput):
        color_status = 1

    return color_status

color_output = check_color(myInput)
print(color_output)
  • Using all to check if all colors in text 使用all检查文本中是否所有颜色
  • elif to check the other conditions. elif检查其他条件。

So I should get as an output the number 1 and 2. 所以我应该将数字1和2作为输出。

No. Once your function reaches a return statement, a value is returned and the function goes no further. 不会。一旦函数到达return语句,就会返回一个值,并且该函数不再执行。

Is it not possible to have 2 if-conditions at the same time? 不能同时拥有2个if条件吗?

Yes. 是。 But if you put a return statement in an if clause which evaluates to True , all subsequent if clauses will be ignored. 但是,如果将return语句放在评估为Trueif子句中,则所有后续if子句都将被忽略。

Without any return --> Output: None 没有任何回报->输出:无

Yes. 是。 If your function does not return or yield anything, it will return None . 如果您的函数不return或不yield任何结果,它将返回None


You need to carefully define what you want as your output in each situation. 您需要仔细定义所需的每种情况的输出。 For example, if you want a list of values as output, initialize a list and append to it. 例如,如果要将值列表作为输出,请初始化list并将其追加到list A dictionary will make your solution easiest to implement and extend. 词典将使您的解决方案最容易实现和扩展。

Here's a demo: 这是一个演示:

myInput = "I like the colors blue and red."

def check_color(var):

    c_map = {'blue': 1, 'red': 2}

    L = []
    for colour, num in c_map.items():
        if colour in var:
            L.append(num)

    return L

print(check_color(myInput))

[1, 2]

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

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