简体   繁体   中英

Removing punctuation from only the beginning and end of each element in a list in python

I'm fairly new to python (and this community), this is a question branching off of a question asked and answered from a long time ago from here

With a list like:

['hello', '...', 'h3.a', 'ds4,']

Creating a new list x with no punctuation (and deleting empty elements) would be:

x = [''.join(c for c in s if c not in string.punctuation) for s in x]
x = [s for s in x if s]
print(x)

Output:

['hello', 'h3a', 'ds4']

However, how would I be able to remove all punctuation only from the beginning and end of each element? I mean, to instead output this:

['hello', 'h3.a', 'ds4']

In this case, keeping the period in the h3a but removing the comma at the end of the ds4.

You could use regular expressions. re.sub() can replace all matches of a regex with a string.

import re
X = ['hello', '.abcd.efg.', 'h3.a', 'ds4,']
X_rep = [re.sub(r"(^[^\w]+)|([^\w]+$)", "", x) for x in X] 
print(X_rep)
# Output: ['hello', 'abcd.efg', 'h3.a', 'ds4']

Explanation of regex: Try it

  • (^[^\\w]+) :
    • ^ : Beginning of string
    • [^\\w]+ : One or more non-word characters
  • | : The previous expression, or the next expression
  • ([^\\w]+$) :
    • [^\\w]+ : One or more non-word characters
    • $ : End of string
x = ['hello', '...', 'h3.a', 'ds4,']
x[0] = [''.join(c for c in s if c not in string.punctuation) for s in x][0]
x[(len(x)-1)] = [''.join(c for c in s if c not in string.punctuation) for s in x][(len(x)-1)]
x = [s for s in x if s]
print(x)

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