0

I have a if statement inside a loop that sets a variable to a specific country name if it matches the condition, that is, if the parameter contains the country name.

The parameters are a list of paths that contain a country name in different positions, i.e. C:\\some\\path\\text_country_text.xlsx or C:\\some\\path\\text_text_country_text.xlsx

The if statement is pretty long at the moment because it checks a rather long list of countries. The code I wrote works but it does not look really optimized. Is there a shorter way to do this?

def my_function(*args): 
    for country in args:
        if "Australia" in country:
            country_name = "Australia"
        elif "Austria" in country:
            country_name = "Austria"
        # etc. for many countries
3
  • 1
    If the country name is in the same position within the paths then just extract it, perhaps comparing it to a list (or dictionary) of the names. Commented Feb 26, 2018 at 10:45
  • Of course this is hugely inefficient, but it would help if you specify more clearly what exactly is the required input and output of the function. Commented Feb 26, 2018 at 10:45
  • @CongMa The input is a list of paths to Excel country tables. The function is supposed to be an automation for inserting values from these tables to a global/general Excel table. This works. I have to put the names of the countries in a column next to the repsective values. I get all values from the tables but the country name is only in the paths and not in the tables. Commented Feb 26, 2018 at 10:54

3 Answers 3

4

Assuming you don't know exactly where the country name is within your country string (so you can't use slicing or a regex to extract the relevant portion), you could adapt your approach to take a list of countries, and then use a generator expression with next() to extract the country name:

next((c_name for c_name in countries if c_name in country), 'Unknown')

Examples:

>>> countries = ['Australia', 'Austria']
>>> country = '#Austrian_man#'
>>> next((c_name for c_name in countries if c_name in country), 'Unknown')
'Austria'
>>> country = '#AustrTYPO_man#'
>>> next((c_name for c_name in countries if c_name in country), 'Unknown')
'Unknown'
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I haven't known about next() so far. I will definitely look into it.
2

You could do: Countries = [ all countries] Code:

for country in args:
  for i in Countries:
        if i in country:
          country_name = i

1 Comment

Many thanks! I haven't thought about this simple solution.
1

Just for the fun of it, and to show the power of sets, it is possible to solve this with only one for loop and a set.intersection() call:

def my_function(*args):
    countries = set(['Austria', 'Australia', 'Brazil', 'Denmark'])
    for country_name in countries.intersection(args):
        print(country_name)

Thus, the loop will only loop over countries that are both among the input arguments and in the predefined countries set.

1 Comment

Thanks! I might adjust my code according to this. My input are 140 Excel tables. It is probably a good idea to be more efficient.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.