简体   繁体   中英

Python: Removing characters from a string and then returning it

For example, given a list of strings prices = ["US$200", "CA$80", "GA$500"] , I am trying to only return ["US", "CA", "GA"] .

Here is my code - what am I doing wrong?

def get_country_codes(prices):
    prices = ""
    list = prices.split()
    list.remove("$")
    "".join(list)
    return list

Since each of the strings in the prices argument has the form '[country_code]$[number]' , you can split each of them on '$' and take the first part.

Here's an example of how you can do this:

def get_country_codes(prices):
    return [p.split('$')[0] for p in prices]

So get_country_codes(['US$200', 'CA$80', 'GA$500']) returns ['US', 'CA', 'GA'] .

Also as a side note, I would recommend against naming a variable list as this will override the built-in value of list , which is the type list itself.

There are multiple problems with your code, and you have to fix all of them to make it work:

def get_country_codes(prices):
    prices = ""

Whatever value your caller passed in, you're throwing that away and replacing it with "" . You don't want to do that, so just get rid of that last line.


    list = prices.split()

You really shouldn't be calling this list list . Also, split with no argument splits on spaces, so what you get may not be what you want:

>>> "US$200, CA$80, GA$500".split()
['US$200,', 'CA$80,', 'GA$500']

I suppose you can get away with having those stray commas, since you're just going to throw them away. But it's better to split with your actual separators, the ', ' . So, let's change that line:

    prices = prices.split(", ")

    list.remove("$")

This removes every value in the list that's equal to the string "$" . There are no such values, so it does nothing.

More generally, you don't want to throw away any of the strings in the list. Instead, you want to replace the strings, with strings that are truncated at the $ . So, you need a loop:

    countries = []
    for price in prices:
        country, dollar, price = price.partition('$')
        countries.append(country)

If you're familiar with list comprehensions, you can rewrite this as a one-liner:

    countries = [price.partition('$')[0] for price in prices]

    "".join(list)

This just creates a new string and then throws it away. You have to assign it to something if you want to use it, like this:

    result = "".join(countries)

But… do you really want to join anything here? It sounds like you want the result to be a list of strings, ['US', 'CA', 'GA'] , not one big string 'USCAGA' , right? So, just get rid of this line.


    return list

Just change the variable name to countries and you're done.

Since your data is structured where the first two characters are the county code you can use simple string slicing.

def get_country_codes(prices):
    return [p[:2] for p in prices]

You call the function sending the prices parameter but your first line initialize to an empty string:

prices = ''

I would also suggest using the '$' character as the split character, like:

list = prices.split('$')

try something like this:

 def get_country_codes(prices): list = prices.split('$') return list[0] 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM