试图计算字符串中的单词 [英] Trying to count words in a string

查看:140
本文介绍了试图计算字符串中的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图分析一个字符串的内容。例如,如果Johnny.Appleseed!是:a * good& farmer被输入为*一个输入然后它应该说有6个字,但我的代码只看到它为0个字。我不确定如何删除不正确的字符。



供参考:我正在使用python 3,我也无法导入任何库

  string = input(type something)
stringss = string.split()

for c in range (len(stringss)):
for stringss [c]:
if(stringss [c] [d] .isalnum!= True):
#something that stringss [c ] [d]
total + = 1
print(words:+ str(total))


<解决方案

基于简单循环的解决方案:



  strs =Johnny。 Appleseed!是:a * good& farmer
lis = []
for crs strs:
if c.isalnum()或c.isspace():
lis。 append(c)
else:
lis.append('')

new_strs =.join(lis)
print new_strs #print'Johnny Appleseed is一个好农民
new_strs.split()#prints ['Johnny','Appleseed','是','a ','good','farmer']



更好的解决方案:



使用正则表达式

 >> ;> import re 
>>>从字符串导入标点符号
>>> strs =Johnny.Appleseed!is:a * good& farmer
>>> r = re.compile(r'[{}]'。格式(标点符号))
>>> 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']


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.

FYI: I'm using python 3, also I can't import any libraries

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))

解决方案

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:

>>> 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']

这篇关于试图计算字符串中的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆