简体   繁体   中英

How to remove a substrings from a list of strings?

I have a list of strings, all of which have a common property, they all go like this "pp:actual_string" . I do not know for sure what the substring "pp:" will be, basically : acts as a delimiter; everything before : shouldn't be included in the result.

I have solved the problem using the brute force approach, but I would like to see a clever method, maybe something like regex.

Note: Some strings might not have this "pp:string" format, and could be already a perfect string, ie without the delimiter.

This is my current solution:

ll = ["pp17:gaurav","pp17:sauarv","pp17:there","pp17:someone"]
res=[]
for i in ll:
    g=""
    for j in range(len(i)):
        if i[j] == ':':
            index=j+1
    res.append(i[index:len(i)])

print(res)

Is there a way that I can do it without creating an extra list?

Here are a few options, based upon different assumptions.

Most explicit

if s.startswith('pp:'):
    s = s[len('pp:'):]  # aka 3

If you want to remove anything before the first :

s = s.split(':', 1)[-1]

Regular expressions:

Same as startswith

s = re.sub('^pp:', '', s)

Same as split, but more careful with 'pp:' and slower

s = re.match('(?:^pp:)?(.*)', s).group(1)

Whilst regex is an incredibly powerful tool with a lot of capabilities, using a "clever method" is not necessarily the best idea you are unfamiliar with its principles.

Your problem is one that can be solved without regex by splitting on the : character using the str.split() method, and just returning the last part by using the [-1] index value to represent the last (or only) string that results from the split. This will work even if there isn't a : .

list_with_prefixes = ["pp:actual_string", "perfect_string", "frog:actual_string"]

cleaned_list = [x.split(':')[-1] for x in list_with_prefixes]
print(cleaned_list)

This is a list comprehension that takes each of the strings in turn ( x ), splits the string on the : character, this returns a list containing the prefix (if it exists) and the suffix, and builds a new list with only the suffix (ie item [-1] in the list that results from the split. In this example, it returns:

['actual_string', 'perfect_string', 'actual_string']

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