猫史档案馆


【Python作品分享】word_count

用户:强乡强乡查看:4 回复:1 评论:4 创建时间:2020-07-21T17:17:23


【作品展示】

center_image

 

【作品介绍】

wordcount is a 排序作品

 

【作品源代码】

def is_symbol(c):
    if c in "!@#$%^&*()_+-={}[]:;"'<,>.?/~`':
        return True
    else:
        return False
def add_count(d,w):
    if w in d:
        d[w]+=1
    else:
        d[w]=1
STATE_START=0
STATE_IN_WORD=1
STATE_NOT_IN_WORD=2
def word_count(s):
    d={}
    start=0
    state=STATE_START
    for i in range(len(s)):
        if state==STATE_START:
            if is_symbol(s[i]):
                state=STATE_NOT_IN_WORD
            else:
                state=STATE_IN_WORD
                start=i
        elif state == STATE_IN_WORD:
            if is_symbol(s[i]):
                state = STATE_NOT_IN_WORD
                w=s[start:i]
                add_count(d,w)
            else:
               pass
        elif state == STATE_NOT_IN_WORD:
            if is_symbol(s[i]):
                pass
            else:
                state = STATE_IN_WORD
                start = i
    if state==STATE_IN_WORD:
        w = s[start:]
        add_count(d, w)
    return d
d=word_count('Luke.like,juice?')
print(d)
assert is_symbol("!")==True
assert is_symbol("?") == True
assert is_symbol("a") == False
assert d['Luke']==1 and d['juice']==1

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 1下一页
128pohxi128pohxi

我给你个喵的,要下载Python

这个!
for i in range(1,喵00000000000000):
    print(i,end='')​

点赞0


评论