Python Forum

Full Version: how to replace OR operator with any
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi Team,

In below code , I am checking Country name if(Ind,US,Aus) then proceed further else exit.
how to use any operator in below situation.

if any("Ind","US","Aus") in Country:  # like this replace or operator in python

Country = "xxx_Ind_xxx"
Country = "xxx_US_xxx"
Country = "xxx_Aus_xxx"

if ("Ind" in Country) or ("US" in Country) or ("Aus" in Country):
	print("Table found proceed further")
else:
	print("Table not found")
	sys.exit
import re
for country in ("xxx_Aus_xxx", "xxx_US_xxx", "xxx_Ind_xxx", "xxx_ICU_xxx"):
    if re.search(r"US|Ind|Aus", country):
        print(country)
But if you heart is set on using any;
mport re

for country in ("xxx_Aus_xxx", "xxx_US_xxx", "xxx_Ind_xxx", "xxx_ICU_xxx"):
    if any(abbr in country for abbr in ("Aus", "US","Ind")):
        print(country)
Hi deanhystad.

Thanks for your help. nice solution.

I found one more solution.
if any(_country in country for _country in ("Ind", "US", "Aus")):
if any(map(country.__contains__, ("Ind", "US", "Aus"))):
Thanks
mg
There is also
if not set(("Ind", "US", "Aus")).isdisjoint(country): ...
(Oct-20-2022, 08:23 PM)Gribouillis Wrote: [ -> ]There is also
if not set(("Ind", "US", "Aus")).isdisjoint(country): ...
Not seeing how this one would work. country will never be "Ind", "US" or "Aus". This doesn't print anything:
for country in ("USA", "Australia", "Italy"):
    if not set(("Ind", "US", "Aus")).isdisjoint(country):
        print(country)
But this prints USA and Australia.
for country in ("USA", "Australia", "Italy"):
    if any(map(country.__contains__, ("Ind", "US", "Aus"))):
        print(country)
(Oct-20-2022, 09:14 PM)deanhystad Wrote: [ -> ]Not seeing how this one would work.
Try
>>> countries = ("USA", "Australia", "Italy")
>>> set(("Ind", "US", "Aus")).isdisjoint(countries)
True
>>> set(("Ind", "USA", "Aus")).isdisjoint(countries)
False
Still not seein how it is useful for this particular problem. OP is checktng if US is a substring of country, not a match.
(Oct-21-2022, 12:11 PM)deanhystad Wrote: [ -> ]US is a substring of country, not a match.
Ah OK sorry I missed that!