首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >统计txt瓦片中的唯一字

统计txt瓦片中的唯一字
EN

Stack Overflow用户
提问于 2019-09-11 11:01:55
回答 4查看 372关注 0票数 0

我有一个输入文件,其中包含莎士比亚的十四行诗(sonnet.txt)。我需要编写简短的代码来计算十四行诗中唯一单词的数量。我的代码必须删除标点符号,忽略小写/大写。

sonnet.txt的内容

代码语言:javascript
复制
How heavy do I journey on the way,
When what I seek, my weary travel's end,
Doth teach that ease and that repose to say,
Thus far the miles are measured from thy friend!
The beast that bears me, tired with my woe,
Plods dully on, to bear that weight in me,
As if by some instinct the wretch did know
His rider loved not speed being made from thee.
The bloody spur cannot provoke him on,
That sometimes anger thrusts into his hide,
Which heavily he answers with a groan,
More sharp to me than spurring to his side;
For that same groan doth put this in my mind,
My grief lies onward, and my joy behind.

我使用set()函数并将结果存储在变量unique_words中。最终目标是使用len(unique_words)计算该集合的长度。

但是,我的代码删除了后面跟着标点符号的单词(即,',';‘’!‘’)。我已经尝试使用过滤函数来删除非字母字符,但是我仍然丢失了后面跟着标点符号的单词。

是否有不同的字符串方法可以与filter()结合使用以获得所需的输出?

提前感谢您的帮助。

代码语言:javascript
复制
unique_words = set()

sonnet = open("sonnet.txt", "r")

for line in sonnet:
    line = [word.lower() for word in line.split()]
    line = [word for word in filter(str.isalpha, line)]
    unique_words.update(line)

sonnet.close()

print("{} unique words".format(len(unique_words)))

第一次理解的结果是

代码语言:javascript
复制
['how', 'heavy', 'do', 'i', 'journey', 'on', 'the', 'way,']

但当我第二次迭代时,这是我得到的输出:

代码语言:javascript
复制
['how', 'heavy', 'do', 'i', 'journey', 'on', 'the']
EN

回答 4

Stack Overflow用户

发布于 2019-09-11 11:47:43

如果字符串中的所有字符都是字母表,则str.isalpha返回true。

  • 输入- 'Mike‘输出-true
  • 输入- 'charlie mike’输出- 'charlie!,‘输出-false

在将isalpha应用于"way“的情况下,返回false。因此,最好在开始时使用string.punctuation删除标点符号,而不需要使用过滤器。

代码语言:javascript
复制
import string
unique_words = set()

sonnet = open("sonnet.txt", "r")

for line in sonnet:
    line ="".join([c for c in line if c not in string.punctuation])
    line = [word.lower() for word in line.split()]
    unique_words.update(line)

sonnet.close()

print("{} unique words".format(len(unique_words))) 

如果需要将"My“和"my”都添加到唯一单词列表中,请不要使用word.lower()

票数 2
EN

Stack Overflow用户

发布于 2019-09-11 11:51:51

我宁愿用不同的方式来做:

代码语言:javascript
复制
import re
from collections import Counter

words = re.findall( r'\w+', text )
counter = Counter( words )
print len(counter)   # prints 95

如果我使用以下命令将所有单词转换为小写:

代码语言:javascript
复制
words = [w.lower() for w in words]

在计数之前,结果是90

票数 1
EN

Stack Overflow用户

发布于 2019-09-11 11:21:04

尽可能接近您的示例,但要用它来修复问题:

代码语言:javascript
复制
unique_words = set()

sonnet = open("sonnet.txt", "r")

for line in sonnet:
    words = ''.join(filter(lambda x: x.isalpha() or x.isspace(), line)).split()
    unique_words.update(words)

sonnet.close()

print("{} unique words".format(len(unique_words)))

不只是检查.isalpha(),您还希望保留空格,以便将它们组合到单个lambda函数中,以按照您的意图使用filter。然后,''.join(generator)将得到的过滤器生成器转换回一个字符串,并对该行进行拆分(在其中的空格上)。

为了清晰起见,结果被称为words,而不是覆盖循环变量line,并且单词被添加到结果中。

输出:

代码语言:javascript
复制
94 unique words
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57881512

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档