【发布时间】:2018-05-21 14:47:29
【问题描述】:
在开始之前,以下是我的学习资料(Grok Learning,Python)中的确切说明 “编写一个程序来读取来自用户的多行输入,其中每一行是一个以空格分隔的单词句子。然后,您的程序应该计算每个二元组在所有输入句子中出现的次数。二元组应该是通过将输入行转换为小写以不区分大小写的方式进行处理。一旦用户停止输入输入,您的程序应打印出出现多次的每个二元组及其对应的频率。"
我应该在几个输入中找到二元组,我已经制定了这段代码。这段代码通过询问输入直到输入为空,然后将整行添加到一个名为组合的列表中,在此将其转换为这种格式的二元组 [('this', 'is'), ('is', ' a')] 等于称为文本的列表。现在,名为 text 的列表被转换为这种格式的简单二元组 [('this is'), ('is a')] 到另一个名为 newlist 的列表中。然后我将所有重复的字符串添加到一个名为 my_dict 的字典中并添加它们。我将它们打印在单独的行中,以便它可以生成每个二元组以及它的频率,不包括只出现一次的二元组。这是我的代码:
newlist = []
combined = []
a = (input("Line: ")).lower()
while a:
combined.append(a)
text = [b for l in combined for b in zip(l.split(" ")[:-1], l.split(" ")[1:])]
a = (input("Line: ")).lower()
for bigram in text:
newbigram = ' '.join(bigram)
newlist.append(newbigram)
my_dict = {i:newlist.count(i) for i in newlist}
for words in sorted(my_dict):
if my_dict[words] > 1:
print(str(words) + str(": ") + str(my_dict[words]))
这是我的输出:
Line: The big red ball
Line: The big red ball is near the big red box
Line: I am near the box
Line:
big red: 3
near the: 2
red ball: 2
the big: 3
看到这段代码运行良好,但是每当我设置一个空值时,它都会出现以下错误消息:
Line:
Traceback (most recent call last):
File "program.py", line 8, in <module>
for bigram in text:
NameError: name 'text' is not defined
为什么会这样,我该如何解决?
【问题讨论】:
-
空字符串不是真实。因此,您无需进入
while循环并定义text -
提示:当
a为空时,while a:块(包括text = ...语句)会运行多少次? -
在开头使用
text = []。所以text是在你有值或设置空值的情况下定义的。
标签: python