简体   繁体   中英

Two conditions loop python

I have data as below

lst = ['abs', '@abs', '&abs']

I need to replace all parts with @ or & . I do like this

new = []
simbol1 = '@'
simbol2 = '&'
for item in lst:
    if simbol1 not in item:
        if simbol2 not in item:
            new.append(item)

But is there more simple way for this loop? I tried like this

lst = ['abs', '@abs', '&abs']
new = []
simbol1 = '@'
simbol2 = '&'
for item in lst:
    if any([simbol1 not in item , simbol2 not in item]):
        new.append(item)

But i get

new
['abs', '@abs', '&abs']

What is a correct way to use multiple conditions in this case?

You can use list comprehension & merge the two if 's as follows:

>>> lst = ['abs', '@abs', '&abs']
>>> new_lst = [l for l in lst if '@' not in l and '&' not in l]
>>> new_lst
['abs']
>>> 

You can also use all() instead of multiple if s like follows:

>>> lst = ['abs', '@abs', '&abs']
>>> new_lst = [l for l in lst if all(x not in l for x in ['@','&'])]
>>> new_lst
['abs']
>>> 

You can just combine two if s:

if simbol1 not in item and simbol2 not in item:
    new.append(item)
lst = ['abs', '@abs', '&abs']
new = []
simbol1 = '@'
simbol2 = '&'
for item in lst:
    if all([simbol1 not in item , simbol2 not in item]):
       new.append(item)
print (new)

Functionally , you could do

new_list = list(filter(lambda x: all(f not in x for f in ['@', '&']), lst))

as an explanation, the lambda function ensures that none of the forbidden f characters are in string by filtering out all values that evaluate to False . filter returns a generator, so one can make that a list .

lst = ['abs', '@abs', '&abs']
out_lst = [el for el in lst if '@' not in el and '&' not in el]
print(out_lst) # ['abc']

Your code is very close to correct; the only problem is that you got the negation backward.

This:

if any([simbol1 not in item , simbol2 not in item]):

… is asking "are any of these are not in the item?" Just as in English, that's true if one is not in the item, but the other is in the item, which isn't what you want. You only want it to be true if neither is in the item.

In other words, you want either "are all of these not in the item?"

if all([simbol1 not in item, simbol2 not in item]):

… or "are none of these in the item?"

if not any([simbol1 in item, simbol2 in item]):

However, when you only have a fixed list of two things like this, it's usually easier to use and or or instead of any or all —again, just like in English:

if symbol1 not in item and simbol2 not in item:

… or:

if not (simbol1 in item or simbol2 in item):

If, on the other hand, you had a whole bunch of symbols to check, or a list of them that you couldn't even know until runtime, you'd want a loop:

if all(simbol not in item for simbol in (simbol1, simbol2)):

if not any(simbol in item for simbol in (simbol1, simbol2)):

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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