0

The task that I have to perform is as follows :

Say I have a list of words (Just an example...the list can have any word):

'yappingly', 'yarding', 'yarly', 'yawnfully', 'yawnily', 'yawning','yawningly', 
'yawweed', 'yealing', 'yeanling', 'yearling', 'yearly', 'yearnfully','yearning', 
'yearnling', 'yeastily', 'yeasting', 'yed',  

I have to create a new list of words from which words having the suffix ing are added after removing the suffix (i.e yeasting is added to the new list as yeast) and the remaining words are added as it is

Now as far as insertion of string ending with ing is concerned, i wrote the following code and it works fine

 Data=[w[0:-3] for w in wordlist if re.search('ing$',w)]

But how to add the remaining words to the list?? How do I add an else clause to the above if statement? I was unable to find suitable documentation for the above. I did came across several questions on SO regarding the shorthand if else statement, but simply adding the else statement at the end of the above code doesn't work. How do I go about it??

Secondly, if I have to extend the above regular expression for multiple suffixes say as follows:

re.search('(ing|ed|al)$',w)

How do I perform the "trim" operation to remove the suffix accordingly and simultaneously add the word to the new list?? Please Help.

alphacentauri
  • 890
  • 3
  • 12
  • 25

2 Answers2

3

Regarding your first question, you can use a ternary placed just before the for:

Data=[w[0:-3] if re.search('ing$',w) else w for w in wordlist]

Regarding your second, well, the best answer in my opinion is to use re.sub as @abarnert demonstrated. However, you could also make a slight adaption to your use of re.search:

Data=[re.search('(.*)(?:ing|ed|al)$', w).group(1) for w in wordlist]

Finally, here is a link for more information on comprehensions.

Community
  • 1
  • 1
  • Ahhh..Thanks...This worked.. Any suggestions where I could read about these not so intuitive python for and if syntax?? I couldn't find a very convincing source – alphacentauri Dec 18 '13 at 21:47
  • @alphacentauri - The link I gave in the answer explains the ternary. Here is a reference on [comprehensions](http://www.openlogic.com/wazi/bid/306769/Python-comprehensions-for-sysadmins). –  Dec 18 '13 at 21:50
  • 1
    @alphacentauri: [PEP 308](http://www.python.org/dev/peps/pep-0308/) describes conditional expressions. The [tutorial](http://docs.python.org/3/tutorial/datastructures.html#list-comprehensions) describes `if` clauses in list comprehensions (and [`if` statements](http://docs.python.org/3/tutorial/controlflow.html#if-statements) if you're confused about those as well else don't bother). – abarnert Dec 18 '13 at 21:51
  • Thank you people...i feel somewhat enchanted..:) – alphacentauri Dec 18 '13 at 21:54
3

First, what makes you think you need a regexp at all? There are easier ways to strip suffixes.

Second, if you want to use regexps, why not just re.sub instead of trying to use regexps and slicing together? For example:

Data = [re.sub('(ing|ed|al)$', '', w) for w in wordlist]

Then you don't need to work out how much to slice off (which would require you to keep track of the result of re.search so you can get the length of the group, instead of just turning it into a bool).

But if you really want to do things your way, just replace your if filter with a conditional expression, as in iCodez's answer.

Finally, if you're stuck on how to fit something into a one-liner, just take it out of the one-liner. It should be easy to write a strip_suffixes function that returns the suffix-stripped string (which is the original string if there was no suffix). Then you can just write:

Data = [strip_suffixes(w) for w in wordlist]
abarnert
  • 313,628
  • 35
  • 508
  • 596
  • Thanks..Obviously there would be easier and elegant methods to perform the above task. It was just that I was learning how to use regex in python, so I guess laying more stress on the use of regex is justified. :) – alphacentauri Dec 18 '13 at 21:46
  • 1
    @alphacentauri: If you're doing this to learn about regexps in Python, I'd definitely suggest writing that `strip_suffixes` function so you can write it in different ways—using `re.sub`, using `re.search` and then getting the length of the matched group, whatever else you can think of. – abarnert Dec 18 '13 at 21:49
  • Will definitely try to do that..Thanks..:) – alphacentauri Dec 18 '13 at 21:50