2

I am learning the intermediate python.

Suppose there's a string

astring = '[Embodied cognition - Wikipedia](https://en.wikipedia.org/wiki/Embodied_cognition)'

Work with string.punctuation to extract the words

from string import punctuation
for delimiter in punctuation:
    if delimiter in astring:
        astring = astring.replace(delimiter, ' ')

In [7]: astring
Out[7]: ' Embodied cognition   Wikipedia  https   en wikipedia org wiki Embodied cognition '

I tried with map and it works

In [12]: a = map(astring.replace, punctuation, ' ')
In [14]: list(a)
Out[14]: [' Embodied cognition   Wikipedia  https   en wikipedia org wiki Embodied cognition ']

How can the problem be solved in other advanced techniques?

2
  • What version of Python are you using? map should be padding your string ' ' argument with None, if I'm reading the docs correctly. Commented Nov 28, 2017 at 3:13
  • sorry, I complement python3.6 tag immediately. @SilvioMayolo Commented Nov 28, 2017 at 3:16

1 Answer 1

4

You should look into regular expressions (regex for short).

import re

astring = '[Embodied cognition - Wikipedia](https://en.wikipedia.org/wiki/Embodied_cognition)'
re.sub(r'[^A-Za-z0-9]+', ' ', astring)
# returns:
' Embodied cognition Wikipedia https en wikipedia org wiki Embodied cognition '
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I learned regex last Saturday. It dramatically improve efficiency to process text.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.