gpt4 book ai didi

python - 前置字长

转载 作者:太空狗 更新时间:2023-10-29 22:29:28 25 4
gpt4 key购买 nike

我必须创建一个函数,该函数采用单个参数 word 并返回文本中 word 之前的单词的平均长度(以字符为单位)。如果单词恰好是文本中出现的第一个单词,则该出现的前一个单词的长度应为零。例如

>>> average_length("the")
4.4
>>> average_length('whale')
False
average_length('ship.')
3.0

这是我到目前为止写的,

def average_length(word):
text = "Call me Ishmael. Some years ago - never mind how long..........."
words = text.split()
wordCount = len(words)

Sum = 0
for word in words:
ch = len(word)
Sum = Sum + ch
avg = Sum/wordCount
return avg

我知道这根本不对,但我不知道如何正确处理这个问题。这道题要求我找出文本中所有 word 的实例,当你这样做时,计算文本中它之前的单词的长度。不是从头到尾的每一个词,只有一个。

我还应该提到所有测试只会使用“Moby Dick”的第一段来测试我的代码:

"Call me Ishmael. Some years ago - never mind how long precisely - having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world. It is a way I have of driving off the spleen and regulating the circulation. Whenever I find myself growing grim about the mouth; whenever it is a damp, drizzly November in my soul; whenever I find myself involuntarily pausing before coffin warehouses, and bringing up the rear of every funeral I meet; and especially whenever my hypos get such an upper hand of me, that it requires a strong moral principle to prevent me from deliberately stepping into the street, and methodically knocking people's hats off - then, I account it high time to get to sea as soon as I can. This is my substitute for pistol and ball. With a philosophical flourish Cato throws himself upon his sword; I quietly take to the ship. There is nothing surprising in this. If they but knew it, almost all men in their degree, some time or other, cherish very nearly the same feelings towards the ocean with me."

最佳答案

似乎只检查一次数据就可以节省大量计算时间:

from collections import defaultdict
prec = defaultdict(list)
text = "Call me Ishmael. Some years ago..".split()

在您的列表上创建两个迭代器。我们在第二个上调用 next,这样从现在开始,每当我们从迭代器中获取一个元素时,我们都会获取一个词及其后继词。

first, second = iter(text), iter(text)
next(second)

压缩两个迭代器 ("abc","def""ad", "be", "cf"),我们附加第一个单词到第二个的前导长度列表。这行得通,因为我们使用的是 defaultdict(list),它会为任何尚不存在的键返回一个空列表。

for one, two in zip(first, second):  # pairwise
prec[two].append(len(one))

最后,我们可以创建一个新的字典,从单词到它们前身长度的平均值:总和除以长度。除了这种字典推导式,您还可以使用普通的 for 循环。

# avg_prec_len = {key: sum(prec[key]) / len(prec[key]) for key in prec}
avg_prec_len = {}
for key in prec:
# prec[key] is a list of lengths
avg[key] = sum(prec[key]) / len(prec[key])

然后你就可以查字典了。

(如果您使用的是 Python 2,请使用 izip 而不是 zip,并执行 from __future__ import division)。

关于python - 前置字长,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36127483/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com