简体   繁体   English

Python 在列表中查找元素并返回 true 或 false

[英]Python find elements in a list and return true or false

I'm in stuck with a research elements in list of strings.我被困在字符串列表中的研究元素中。 I take a text with numbers(string) and I try to find symbols like '+' or '-' My code:我拿了一个带有数字(字符串)的文本,然后尝试找到像“+”或“-”这样的符号我的代码:

 Lista=np.array([s for s in text]) 
 y=np.where(Lista=='+') 
 if y==[]: /*so, if it's void*/
        print("any elements")
  else:
        print("yes")

I try to do this, because I'd like to create a calculator, and for this moment I can do just + operation (in second part of my code, I use split("+") and cast string to int).我尝试这样做,因为我想创建一个计算器,现在我只能做 + 操作(在我的代码的第二部分,我使用 split("+") 并将字符串转换为 int)。 So I'd like to create with if, else, elif a struct similar to switch case.所以我想用 if, else, elif 创建一个类似于 switch case 的结构。 Something like: if ('+') -> do this operation, else if ('-')-> do this operation, ecc.类似:if ('+') -> 做这个操作,else if ('-')-> 做这个操作,ecc。 Obviously, I'd like to create 4 y=np.where(lista='+'/='-'/=' '...) and for everyone create if else.显然,我想创建 4 y=np.where(lista='+'/='-'/=' '...) 并为每个人创建 if else。 So if in thirst case I don't find '+':pass.因此,如果在口渴的情况下我找不到“+”:pass。 If in second case I don't find '-' :pass.如果在第二种情况下我没有找到“-”:pass。 If in third case I find ' ' : do the moltiplicato.如果在第三种情况下我发现 ' ' :做 moltiplicato。 This things I try to do with y==[] or y!=[].我尝试用 y==[] 或 y!=[] 来做这些事情。 When I print the results of y=np.where(lista='+'), so I see that it returns array[1] or array[], but if I try to make this as a condition, it doesn't work.当我打印 y=np.where(lista='+') 的结果时,我看到它返回 array[1] 或 array[],但是如果我尝试将其作为条件,则它不起作用.

Example.例子。 In my calculator I wrote 4+5.在我的计算器中,我写了 4+5。 4+5 it's saved in a var called text. 4+5 它保存在一个名为 text 的 var 中。 I convert the var text, in Lista=np.array([s for s in text]).我在 Lista=np.array([s for s in text]) 中转换 var 文本。 After I try to search in Lista the symbol '+' with y=np.where(Lista=='+').在我尝试在 Lista 中搜索符号 '+' 后,y=np.where(Lista=='+')。 In console I see that it's at first position, so in array[1].在控制台中,我看到它位于第一个位置,因此在数组 [1] 中。 So with if condition所以如果条件

    if y==[]: 
       print("the list it's void") 
       pass
    else: print("the elemnt it's at firt position")
    Lista=np.array([s for s in text.split('+')])
    v=[int(v) for v in Lista]
    c=sum(v)
    print(c)

But, when I wrote 4+5, with the condition y==[] it's print that the list it's void, but it doesnt true because at arrayIndex I see that in position y[1] ther're a '+'.但是,当我写 4+5 时,在条件 y==[] 的情况下,它打印出列表为空,但它不是真的,因为在 arrayIndex 我看到在位置 y[1] 有一个“+”。

Example in Java code. Java 代码中的示例。 I'd like to have the same in Python public class Main {我想在 Python public class Main { 中也有相同的

    public static void main(String[] args) {
        boolean findSymbol=true;

        char [] p = {31, 28, 31, 30, 31, 30, 31, '-'};
        for (char c : p) {
            if (c == '+') {
                findSymbol= true;
                System.out.println("let's do something");
            } else
                findSymbol = false;
            System.out.println("Nope");

            if (c == '-') {
                findSymbol=true;
                System.out.println("let's do something");
                
            }else
                findSymbol = false;
            System.out.println("Nope");

            }
        }

}

Python 3.10 has a match statement which is the pythonic version of a switch . Python 3.10 有一个match语句,它是switch的 Pythonic 版本。 Anyhow as you said you can get the same result with if ... elif ... else :无论如何,正如您所说,您可以使用if ... elif ... else获得相同的结果:

if s=='+':
    ##handle addition
elif s=='-':
    ##handle subtraction
elif s=='*':
    ##handle multiplication
##etc
else:
    ##did not match any previous check, raise exception or something

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

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