python - 如何在python的几个输入中计算二元组
问题描述
在我开始之前,这里是我的学习材料(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
为什么会这样,我该如何解决?
解决方案
您的主要问题是由于空输入实际上包含 的值\n
,因此您的while
语句评估为True
。您可以剥离用户输入,以便不包含尾随/前导空格,因为无论如何它对您的使用都是无关紧要的,例如:
a = input("Line: ").strip().lower()
如果用户从不输入任何内容(只有一个空行),您的text
列表将永远不会被初始化,因此在while
循环之前对其进行初始化。
话虽如此,您对此过于复杂了-您只需要一个字典计数器并遍历输入的元组以增加计数,例如:
import collections
counter = collections.defaultdict(int) # use a dictionary factory to speed up our counting
while True:
line = input("Line: ").lower().split() # lowercase and split on whitespace
if not line: # user entered an empty line
break
for bigram in zip(line, line[1:]): # iterate over bigrams
counter[bigram] += 1 # increase the count of each bigram
for bigram, count in counter.items(): # use counter.iteritems() on Python 2.x
if count > 1: # consider only bigrams with larger count than 1
print("{} {}: {}".format(bigram[0], bigram[1], count))
推荐阅读
- maven - 创建了drools Kie会话,我得到了这个“找不到类异常”
- spring - 引起:java.lang.IllegalStateException:你需要为git仓库配置一个uri
- python - 在excel中将值打印到新行
- python - 如何在 PySpark Word2vec 模型中设置迭代次数?
- android - 子类化房间实体
- c - c - 无法理解 pthread_join()
- python - 我如何理解我是否准确地解析了网站?
- hive - 如何为蜂巢中的点后的位数建立分布表?
- excel - 如何复制和粘贴值(Excel VBA)?
- prolog - 检查变量是否在确定性时间内接地/实例化