在Python中进行AND/OR? [英] AND/OR in Python?

查看:82
本文介绍了在Python中进行AND/OR?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道andor表达式存在于python中,但是是否有任何and/or表达式?还是通过某种方式将它们组合在一起以产生与and/or表达式相同的效果?

I know that the and and or expressions exist in python, but is there any and/or expression? Or some way to combine them in order to produce the same effect as a and/or expression?

我的代码如下:

if input=="a":        
    if "a" or "á" or "à" or "ã" or "â" in someList:            
        someList.remove("a") or someList.remove("á") or someList.remove("à") or someList.remove("ã") or someList.remove("â")

用这个,我的意思是,如果用户输入"a"并且任何类型的"a"都包含在先前定义的列表中,我是否可以从给定列表中删除所有类型的"a"?

with this, I mean that if the user inputs "a" and any type of "a" is included in a previously defined list, can I have all the types of "a" removed from a given list?

python告诉我存在问题:

python tells me that there is a problem in:

someList.remove("a") or someList.remove("á") or someList.remove("à") or someList.remove("ã") or someList.remove("â")

他告诉我:ValueError: list.remove(x): x not in list

推荐答案

Matt Ball 的回答说明,or 和/或".但是or与上面使用in的方式不兼容.您必须说if "a" in someList or "á" in someList or....还是更好,

As Matt Ball's answer explains, or is "and/or". But or doesn't work with in the way you use it above. You have to say if "a" in someList or "á" in someList or.... Or better yet,

if any(c in someList for c in ("a", "á", "à", "ã", "â")):
    ...

这就是您所问问题的答案.

That's the answer to your question as asked.

但是,关于您已发布的示例代码,还有更多要说的话.首先,这里的someList.remove... or someList remove...语句链是不必要的,并且可能导致意外行为.这也很难读!最好将其分成几行:

However, there are a few more things to say about the example code you've posted. First, the chain of someList.remove... or someList remove... statements here is unnecessary, and may result in unexpected behavior. It's also hard to read! Better to break it into individual lines:

someList.remove("a")
someList.remove("á")
...

但是,这还不够.如您所见,如果该项目不在列表中,那么将引发错误.最重要的是,使用remove的速度非常慢,因为每次调用它时,Python都必须查看列表中的每个项目.因此,如果要删除10个不同的字符,并且列表中包含100个字符,则必须执行1000个测试.

Even that's not enough, however. As you observed, if the item isn't in the list, then an error is thrown. On top of that, using remove is very slow, because every time you call it, Python has to look at every item in the list. So if you want to remove 10 different characters, and you have a list that has 100 characters, you have to perform 1000 tests.

相反,我建议一种非常不同的方法.使用set过滤列表,如下所示:

Instead, I would suggest a very different approach. Filter the list using a set, like so:

chars_to_remove = set(("a", "á", "à", "ã", "â"))
someList = [c for c in someList if c not in chars_to_remove]

或者,就地更改列表而不创建副本:

Or, change the list in-place without creating a copy:

someList[:] = (c for c in someList if c not in chars_to_remove)

这两个都使用列表理解语法来创建新列表.他们查看someList中的每个字符,检查该字符是否位于chars_to_remove中,如果不是,则将该字符包括在新列表中.

These both use list comprehension syntax to create a new list. They look at every character in someList, check to see of the character is in chars_to_remove, and if it is not, they include the character in the new list.

这是此代码的最有效版本.它具有两个速度优势:

This is the most efficient version of this code. It has two speed advantages:

  1. 它仅通过someList一次.在上述情况下,它没有执行1000个测试,而是仅执行100个.
  2. 它可以通过一次操作测试所有字符,因为chars_to_removeset.如果chars_to_removelisttuple,则在上述情况下每个测试实际上将是10个测试-因为列表中的每个字符都需要单独检查.
  1. It only passes through someList once. Instead of performing 1000 tests, in the above scenario, it performs only 100.
  2. It can test all characters with a single operation, because chars_to_remove is a set. If it chars_to_remove were a list or tuple, then each test would really be 10 tests in the above scenario -- because each character in the list would need to be checked individually.

这篇关于在Python中进行AND/OR?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆