简体   繁体   中英

How do I remove a part of a string?

I have a list of cities in the United States (some elements: 'Midlothian, VA', 'Ridley Park, PA', 'Johannesberg, CA', 'Sylva, NC', 'Kewwannee, IL', 'Superior, WI', 'Brewster, OH', 'Stillwater, MN' ). I've been trying to remove anything after the comma (since I only want the name, not the state). I tried many things, but I can't get it to work. For example:

for i in US_cities_list:
    i = i.split(",")
    if len(i) == 2:
        x = i.pop(1)
        i = x

This still gives me the same output ( 'Midlothian, VA', 'Ridley Park, PA', 'Johannesberg, CA', 'Sylva, NC', 'Kewwannee, IL', 'Superior, WI', 'Brewster, OH', 'Stillwater, MN' ). Any help would be appreciated, thanks.

The problem with your solution is that assigning i = x doesn't modify the original list. You can see this by adding print(US_citites_list) inside your loop and observe that the list never changes.

One solution is to build a new list with the modified strings:

cities_only = [i.split(",")[0] for i in US_cities_list]

Note that I take advantage of the fact that i.split() will always return a list of at least one element. If there is no comma in the string, there will only be one element. If there is a comma, then there will be two elements. This means there is no need to check the length of i .

A regex replacement works nicely here:

inp = ['Midlothian, VA', 'Ridley Park, PA', 'Johannesberg, CA', 'Sylva, NC', 'Kewwannee, IL', 'Superior, WI', 'Brewster, OH', 'Stillwater, MN']
output = [re.sub(r',.*', '', x) for x in inp]
print(output)

This prints:

['Midlothian', 'Ridley Park', 'Johannesberg', 'Sylva', 'Kewwannee', 'Superior',
 'Brewster', 'Stillwater']

As Code-Apprentice pointed out, assigning i = x doesn't modify the original list.

One method of actually modifying the original list while keeping as much of your original code as possible would be:

for i, item in enumerate(US_cities_list):
    item = item.split(",")
    US_cities_list[i] = item[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