Posts: 119
Threads: 66
Joined: Sep 2022
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
Posts: 6,779
Threads: 20
Joined: Feb 2020
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)
Posts: 119
Threads: 66
Joined: Sep 2022
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
Posts: 4,783
Threads: 76
Joined: Jan 2018
There is also
if not set(("Ind", "US", "Aus")).isdisjoint(country): ...
Posts: 6,779
Threads: 20
Joined: Feb 2020
(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)
Posts: 4,783
Threads: 76
Joined: Jan 2018
(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
Posts: 6,779
Threads: 20
Joined: Feb 2020
Still not seein how it is useful for this particular problem. OP is checktng if US is a substring of country, not a match.
Posts: 4,783
Threads: 76
Joined: Jan 2018
(Oct-21-2022, 12:11 PM)deanhystad Wrote: US is a substring of country, not a match. Ah OK sorry I missed that!
|