Python 尝试计算字符串中的单词
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17507876/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Trying to count words in a string
提问by Harry Harry
I'm trying to analyze the contents of a string. If it has a punctuation mixed in the word I want to replace them with spaces.
我正在尝试分析字符串的内容。如果单词中混有标点符号,我想用空格替换它们。
For example, If Johnny.Appleseed!is:a*good&farmer is entered as an input then it should say there are 6 words, but my code only sees it as 0 words. I'm not sure how to remove an incorrect character.
例如,如果 Johnny.Appleseed!is:a*good&farmer 作为输入输入,那么它应该说有 6 个单词,但我的代码只将其视为 0 个单词。我不确定如何删除不正确的字符。
FYI: I'm using python 3, also I can't import any libraries
仅供参考:我正在使用 python 3,也无法导入任何库
string = input("type something")
stringss = string.split()
for c in range(len(stringss)):
for d in stringss[c]:
if(stringss[c][d].isalnum != True):
#something that removes stringss[c][d]
total+=1
print("words: "+ str(total))
采纳答案by Ashwini Chaudhary
Simple loop based solution:
基于简单循环的解决方案:
strs = "Johnny.Appleseed!is:a*good&farmer"
lis = []
for c in strs:
if c.isalnum() or c.isspace():
lis.append(c)
else:
lis.append(' ')
new_strs = "".join(lis)
print new_strs #print 'Johnny Appleseed is a good farmer'
new_strs.split() #prints ['Johnny', 'Appleseed', 'is', 'a', 'good', 'farmer']
Better solution:
更好的解决方案:
Using regex
:
使用regex
:
>>> import re
>>> from string import punctuation
>>> strs = "Johnny.Appleseed!is:a*good&farmer"
>>> r = re.compile(r'[{}]'.format(punctuation))
>>> new_strs = r.sub(' ',strs)
>>> len(new_strs.split())
6
#using `re.split`:
>>> strs = "Johnny.Appleseed!is:a*good&farmer"
>>> re.split(r'[^0-9A-Za-z]+',strs)
['Johnny', 'Appleseed', 'is', 'a', 'good', 'farmer']
回答by Rushy Panchal
for ltr in ('!', '.', ...) # insert rest of punctuation
stringss = strings.replace(ltr, ' ')
return len(stringss.split(' '))
回答by Prashant Kumar
Here's a one-line solution that doesn't require importing any libraries.
It replaces non-alphanumeric characters (like punctuation) with spaces, and then split
s the string.
这是一种不需要导入任何库的单行解决方案。
它用空格替换非字母数字字符(如标点符号),然后split
是字符串。
Inspired from "Python strings split with multiple separators"
灵感来自“用多个分隔符分割的 Python 字符串”
>>> s = 'Johnny.Appleseed!is:a*good&farmer'
>>> words = ''.join(c if c.isalnum() else ' ' for c in s).split()
>>> words
['Johnny', 'Appleseed', 'is', 'a', 'good', 'farmer']
>>> len(words)
6
回答by Dotan
try this: it parses the word_list using re, then creates a dictionary of word:appearances
试试这个:它使用 re 解析 word_list,然后创建一个 word:appearances 字典
import re
word_list = re.findall(r"[\w']+", string)
print {word:word_list.count(word) for word in word_list}
回答by TMoover
I know that this is an old question but...How about this?
我知道这是一个老问题,但是...怎么样?
string = "If Johnny.Appleseed!is:a*good&farmer"
a = ["*",":",".","!",",","&"," "]
new_string = ""
for i in string:
if i not in a:
new_string += i
else:
new_string = new_string + " "
print(len(new_string.split(" ")))
回答by sweet_sugar
How about using Counter from collections ?
从集合中使用 Counter 怎么样?
import re
from collections import Counter
words = re.findall(r'\w+', string)
print (Counter(words))
回答by alien ware
#Write a python script to count words in a given string.
s=str(input("Enter a string: "))
words=s.split()
count=0
for word in words:
count+=1
print(f"total number of words in the string is : {count}")