-1

The python code below reads 'resting-place' as one word.
The modified list shows up as: ['This', 'is', 'my', 'resting-place.']
I want it to show as: ['This', 'is', 'my', 'resting', 'place']

Thereby, giving me a total of 5 words instead of 4 words in the modified list.

original = 'This is my resting-place.'
modified = original.split()
print(modified)

numWords = 0
for word in modified:
    numWords += 1

print ('Total words are:', numWords)

Output is:

Total words are: 4

I want the output to have 5 words.

myverdict
  • 622
  • 8
  • 24
  • well split at `'-'` too if this what you want... `numWords = sum(len(word.split('-')) for word in modified)` – Julien Apr 18 '18 at 06:36
  • @mij contd from above comment: so, I wanted to remove the "-" and read resting-place as two words, instead of one. – myverdict Apr 27 '18 at 03:34
  • 1
    Yes, there are no spaces between those words in your string, however this is insignificant. The top answer on that question removes spaces and punctuation from the string to give only the words, no matter how many of each there is in-between. Using the answer from that question on your string, `re.findall(r"[\w']+", original)` gives `['This', 'is', 'my', 'resting', 'place']`. – mij Apr 27 '18 at 07:06

4 Answers4

2

To count number of words in a sentence with - separates to two words without splitting:

>>> original = 'This is my resting-place.'
>>> sum(map(original.strip().count, [' ','-'])) + 1
5
Austin
  • 25,759
  • 4
  • 25
  • 48
  • This would not give an accurate answer as you are adding one every time. The answer would have been incorrect if the string had more hyphenated words. This solution does not work for string possibilities having more than one hyphenated pair of words. – myverdict Jun 02 '19 at 15:23
  • @SamVitare, please give an example of failing scenario. – Austin Jun 02 '19 at 15:31
  • Hi, I apologize for my mistake, when I changed the original = 'This is my resting-place peter-pan.', I don't know why I got 6 as the answer instead of 7. But I ran in again and I got the answer right. I must have mistyped something. – myverdict Jun 02 '19 at 15:48
  • For this input, my code outputs 7 (There are 7 words). – Austin Jun 02 '19 at 15:50
  • No worries. It is actually good that you have come back after one year to mark an answer as accepted. SO is grateful to you. :) – Austin Jun 02 '19 at 15:56
  • Ha ha, have been quiet busy. Was just going through all my posts and checking out which answers worked for me. – myverdict Jun 02 '19 at 15:56
2

Here is the code:

s='This is my resting-place.'
len(s.split(" "))

4
Avinash
  • 485
  • 1
  • 7
  • 15
1

You can use regex:

import re
original = 'This is my resting-place.'
print(re.split("\s+|-", original))

Output:

['This', 'is', 'my', 'resting', 'place.']
Rakesh
  • 81,458
  • 17
  • 76
  • 113
0

I think you will find what you want in this article, here you can find how to create a function where you can pass multiple parameter to split a string, in your case you'll be able to split that extra character

http://code.activestate.com/recipes/577616-split-strings-w-multiple-separators/

here is an example of the final result

>>> s = 'thing1,thing2/thing3-thing4'
>>> tsplit(s, (',', '/', '-'))
>>> ['thing1', 'thing2', 'thing3', 'thing4']
Lucas Tambarin
  • 395
  • 3
  • 14