Python Forum
how to replace OR operator with any
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to replace OR operator with any
#1
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
Reply
#2
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)
Reply
#3
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
Reply
#4
There is also
if not set(("Ind", "US", "Aus")).isdisjoint(country): ...
Reply
#5
(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)
Reply
#6
(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
Reply
#7
Still not seein how it is useful for this particular problem. OP is checktng if US is a substring of country, not a match.
Reply
#8
(Oct-21-2022, 12:11 PM)deanhystad Wrote: US is a substring of country, not a match.
Ah OK sorry I missed that!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Search & Replace - Newlines Added After Replace dj99 3 3,450 Jul-22-2018, 01:42 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020