我必须创建一个函数,该函数接受单个参数单词并返回文本中单词之前的单词的平均长度(以字符为单位)。如果单词恰好是文本中出现的第一个单词,则该单词的前一个单词的长度应为零。例如
>>> 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
我知道这根本不对,但是我在正确解决这个问题上遇到了麻烦。这个问题是要我在文本中找到单词的每个实例,并且在执行此操作时,请计算文本中紧接该单词之前的单词的长度。从开始到单词,不是每个单词,只有一个。
我还应该提到,所有测试都只会使用“Moby Dick”的第一段来测试我的代码:
最佳答案
根据您对不导入和简单方法的要求,以下函数可以完全不执行任何操作,注释和变量名应使函数逻辑清晰明了:
def match_previous(lst, word):
# keep matches_count of how many times we find a match and total lengths
matches_count = total_length_sum = 0.0
# pull first element from list to use as preceding word
previous_word = lst[0]
# slice rest of words from the list
# so we always compare two consecutive words
rest_of_words = lst[1:]
# catch where first word is "word" and add 1 to matches_count
if previous_word == word:
matches_count += 1
for current_word in rest_of_words:
# if the current word matches our "word"
# add length of previous word to total_length_sum
# and increase matches_count.
if word == current_word:
total_length_sum += len(previous_word)
matches_count += 1
# always update to keep track of word just seen
previous_word = current_word
# if matches_count is 0 we found no word in the text that matched "word"
return total_length_sum / matches_count if matches_count else False
它需要两个参数,单词的拆分列表和要搜索的单词:
In [41]: text = "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 previous_wordent me from deliberately stepping into the street, and methodically knocking people's hats off - then, I acmatches_count 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."
In [42]: match_previous(text.split(),"the")
Out[42]: 4.4
In [43]: match_previous(text.split(),"ship.")
Out[43]: 3.0
In [44]: match_previous(text.split(),"whale")
Out[44]: False
In [45]: match_previous(text.split(),"Call")
Out[45]: 0.0
很明显,您可以执行与自己的函数相同的操作,只需一个arg即可在函数中拆分文本。返回False的唯一方法是,如果我们找不到该单词的匹配项,则可以看到call返回0.0,因为它是文本中的第一个单词。
如果我们在代码中添加一些图片并使用enumerate:
def match_previous(lst, word):
matches_count = total_length_sum = 0.0
previous_word = lst[0]
rest_of_words = lst[1:]
if previous_word == word:
print("First word matches.")
matches_count += 1
for ind, current_word in enumerate(rest_of_words, 1):
print("On iteration {}.\nprevious_word = {} and current_word = {}.".format(ind, previous_word, current_word))
if word == current_word:
total_length_sum += len(previous_word)
matches_count += 1
print("We found a match at index {} in our list of words.".format(ind-1))
print("Updating previous_word from {} to {}.".format(previous_word, current_word))
previous_word = current_word
return total_length_sum / matches_count if matches_count else False
并使用一个小的示例列表运行它,我们可以看到发生了什么:
In [59]: match_previous(["bar","foo","foobar","hello", "world","bar"],"bar")
First word matches.
On iteration 1.
previous_word = bar and current_word = foo.
Updating previous_word from bar to foo.
On iteration 2.
previous_word = foo and current_word = foobar.
Updating previous_word from foo to foobar.
On iteration 3.
previous_word = foobar and current_word = hello.
Updating previous_word from foobar to hello.
On iteration 4.
previous_word = hello and current_word = world.
Updating previous_word from hello to world.
On iteration 5.
previous_word = world and current_word = bar.
We found a match at index 4 in our list of words.
Updating previous_word from world to bar.
Out[59]: 2.5
使用
iter
的好处是我们不需要通过切余数来创建新列表,只需在函数的开始处将其更改为即可在代码中使用它:def match_previous(lst, word):
matches_count = total_length_sum = 0.0
# create an iterator
_iterator = iter(lst)
# pull first word from iterator
previous_word = next(_iterator)
if previous_word == word:
matches_count += 1
# _iterator will give us all bar the first word we consumed with next(_iterator)
for current_word in _iterator:
每次您使用迭代器中的元素时,我们都会移至下一个元素:
In [61]: l = [1,2,3,4]
In [62]: it = iter(l)
In [63]: next(it)
Out[63]: 1
In [64]: next(it)
Out[64]: 2
# consumed two of four so we are left with two
In [65]: list(it)
Out[65]: [3, 4]
dict真正有意义的唯一方法是,如果您对函数使用多个单词(可以使用*args来完成):
def sum_previous(text):
_iterator = iter(text.split())
previous_word = next(_iterator)
# set first k/v pairing with the first word
# if "total_lengths" is 0 at the end we know there
# was only one match at the very start
avg_dict = {previous_word: {"count": 1.0, "total_lengths": 0.0}}
for current_word in _iterator:
# if key does not exist, it creates a new key/value pairing
avg_dict.setdefault(current_word, {"count": 0.0, "total_lengths": 0.0})
# update value adding word length and increasing the count
avg_dict[current_word]["total_lengths"] += len(previous_word)
avg_dict[current_word]["count"] += 1
previous_word = current_word
# return the dict so we can use it outside the function.
return avg_dict
def match_previous_generator(*args):
# create our dict mapping words to sum of all lengths of their preceding words.
d = sum_previous(text)
# for every word we pass to the function.
for word in args:
# use dict.get with a default of an empty dict.
# to catch when a word is not in out text.
count = d.get(word, {}).get("count")
# yield each word and it's avg or False for non existing words.
yield (word, d[word]["total_lengths"] / count if count else False)
然后只需输入文本和您要搜索的所有单词,就可以在generator function上调用list:
In [69]: list(match_previous_generator("the","Call", "whale", "ship."))
Out[69]: [('the', 4.4), ('Call', 0.0), ('whale', False), ('ship.', 3.0)]
或遍历它:
In [70]: for tup in match_previous_generator("the","Call", "whale", "ship."):
....: print(tup)
....:
('the', 4.4)
('Call', 0.0)
('whale', False)
('ship.', 3.0)
关于python - 前置字长,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36127483/