0

I know the desired syntax lies in the first function but I for the life of me can't find where it is.

I've attempted to remove commas and add spaces to each .split() each has yielded an undesired return value.

def get_country_codes(prices):
    price_list = prices.split(',')  
    results = ''
    for price in price_list:  
        results += price.split('$')[0]   
    return results

def main():
    prices = "US$40, AU$89, JP$200"
    price_result = get_country_codes(prices)
    print(price_result)

if __name__ == "__main__":
    main()

The current output:

US AU JP

The desired output:

US, AU, JP
Ralf
  • 16,086
  • 4
  • 44
  • 68
  • please fix the code indentations, as the code example can not be run like this. – Ralf May 29 '19 at 18:15
  • 1
    Also, are you using Python 2 or 3? Don't just link both tags without a good reason to do so. There is also just the `Python` tag if the version does not matter – Ralf May 29 '19 at 18:16
  • Thanks for formatting your code after being asked to do so; not many users do so. – Ralf May 29 '19 at 18:25
  • `price_result = re.sub('\$\d+', '', prices)` – chepner May 29 '19 at 18:39

3 Answers3

0

It looks like you could benefit from using a list to collect the country codes of the prices instead of a string. Then you can use ', '.join() later.

Maybe like this:

def get_country_codes(prices):
    country_code_list = []

    for price in prices.split(','):
        country_code = price.split('$')[0].strip()
        country_code_list.append(country_code)

    return country_code_list

if __name__ == '__main__':
    prices = "US$40, AU$89, JP$200"
    result_list = get_country_codes(prices)
    print(', '.join(result_list))

Or if you like really short code:

prices = "US$40, AU$89, JP$200"
print(
    ', '.join(
        price.split('$')[0].strip()
        for price in prices.split(',')))
Ralf
  • 16,086
  • 4
  • 44
  • 68
0

You could also use regex if you want to. Since you know country codes will be two capital letters only (A-Z), you can look for a match of two capital letters that precede a dollar sign.

def get_country_codes(prices):
    country_codes = re.findall(r'([A-Z]{2})\$', prices)
    return ', '.join(country_codes)

See regex demo here.

Endyd
  • 1,249
  • 6
  • 12
0

Look at the successive steps:

Your string:

In [1]: prices = "US$40, AU$89, JP$200"                                              

split into a list on comma

In [2]: alist = prices.split(',')                                                    
In [3]: alist                                                                        
Out[3]: ['US$40', ' AU$89', ' JP$200']

split the substrings on $

In [4]: [price.split('$') for price in alist]                                        
Out[4]: [['US', '40'], [' AU', '89'], [' JP', '200']]

select the first element:

In [5]: [price.split('$')[0] for price in alist]                                     
Out[5]: ['US', ' AU', ' JP']

Your += joins the strings as is; same as join with ''. Note that the substrings still have the initial blank for the original string.

In [6]: ''.join([price.split('$')[0] for price in alist])                            
Out[6]: 'US AU JP'

Join with comma:

In [7]: ','.join([price.split('$')[0] for price in alist])                           
Out[7]: 'US, AU, JP'

join is the easiest way of joining a list of strings with a specific delimiter between, in effect reversing a split. += in a loop is harder to use, since it tends to add an extra delimiter at the start or end.

hpaulj
  • 221,503
  • 14
  • 230
  • 353