python-3.x - 我有一个关于我的程序的问题,它读取用户输入的 2 个句子,然后以各种方式比较它们
问题描述
我是 Python 的新手,我正在尝试编写一个程序来读取用户提供的两个句子,然后通过以下方式比较它们:
I. 它必须显示两个句子中包含的所有唯一词的列表。
二、它必须显示出现在两个句子中的单词列表。
三、它必须显示出现在第一个句子中而不是第二个句子中的单词列表。
四。它必须显示出现在第二个句子但不是第一个句子中的单词列表。
V. 它必须显示出现在第一句或第二句中的单词列表,但不能同时显示。
我尝试在此站点上搜索我的问题,但找不到任何能准确描述我的问题的内容。我尝试阅读我的书(Learning Python - 5th Ed),甚至在网上搜索有关如何让我的程序正常运行的指针。
这是我的代码,我提前道歉,因为我知道这不是处理此类程序的最有效方法;但我是 Python 的新手,我想不出更好的方法:
def 语句显示():
userSentence1 = input('Please enter your first sentence: ')
userSentence2 = input('Please enter your second sentence: ')
sentDisplay1 = userSentence1.split()
sentDisplay2 = userSentence2.split()
print('The words contained within sentence #1 is: ', sentDisplay1)
print('The words contained within sentence #2 is: ', sentDisplay2)
句子显示()
def sent1And2Display():
userSent1 = input('Please enter your first sentence: ')
userSent2 = input('Please enter your second sentence: ')
displaySent1And2 = set(userSent1).union(set(userSent2))
displaySent1And2 = (userSent1.split() + userSent2.split())
print('The words contained within both sentence #1 and #2 is: ' + str(displaySent1And2))
发送1And2显示()
def diffOfUnion():
uSent1 = input('Please enter your first sentence: ')
uSent2 = input('Please enter your second sentence: ')
set1And2 = [set(uSent1), set(uSent2)]
nonRepSet = []
for sentSetElm in set1And2:
nonRepSet.append(len(sentSetElm.difference(set.union(*[i for i in set1And2 if i is not sentSetElm]))))
print('The unique words contained within both sentences is: ', (nonRepSet))
diffOfUnion()
def symmDiff():
symmSent1 = input('Please enter your first sentence: ')
symmSent2 = input('Please enter your second sentence: ')
symmDiffSent1And2 = set(symmSent1).symmetric_difference(set(symmSent2))
symmDiffSent1And2 = (symmSent1.split()+ symmSent2.split())
print('The words contained in either sentence #1 and #2, but not both is: ' + str(symmDiffSent1And2))
符号差异()
我知道使用集合操作是我需要做的,但我的函数(#3 和#4)并没有表现出应有的行为;函数 #3 将答案显示为一组整数,但我需要它像我的其他函数一样显示为一组字符串。此外,函数#4 没有找到两个句子的对称差异,我不太明白为什么。
对于我做错的任何帮助将不胜感激,谢谢。
解决方案
短而甜。
sent1 = "A random sentence.".split()
sent2 = "Some other random sentence.".split()
diff = set(sent1) ^ set(sent2)
union = set(sent1) & set(sent2)
one_not_two = set(sent1) - set(sent2)
two_not_one = set(sent2) - set(sent1)
all_words = sent1 + sent2
print(f"I. The word(s) contained in both sentence #1 and #2: {', '.join(union)}")
print(f"II. All word(s) from either: {', '.join(all_words)}")
print(f"III. The word(s) contained in sentence #1 only: {', '.join(one_not_two)}")
print(f"IV. The word(s) contained in sentence #2 only: {', '.join(two_not_one)}")
print(f"V.The word(s) contained in either sentence #1 or #2, but not both: {', '.join(diff)}")
I. 句子#1 和#2 中包含的单词:sentence., random
二、任何一个中的所有单词:A,随机,句子。,一些,其他,随机,句子。
三、仅第 1 句中包含的单词:A
四。仅在句子 #2 中包含的单词:Some, other
V. 句子#1 或#2 中包含的单词,但不能同时包含:A、Some、other
推荐阅读
- python - 循环通过逻辑回归参数以找到最佳模型,但无法复制它
- php - Composer 要求内存不足。PHP 致命错误:允许的内存大小为 1610612736 字节已用尽 Voyager
- performance - 如何减少 tensorflow 数据集输入管道主机设备 (cpu) 时间(目前约为 40%)?
- bootstrap-4 - 使用 Bootstrap 有条件地更改列的布局
- python - MongoDB 聚合属性模式管道/查询
- javascript - 页面重新加载/刷新后如何通过搜索功能执行本地存储值
- java - 从 DescribeSObjectResult 转换为 JsonArray(或 HttpEntity)
- c# - 类、结构或接口成员声明中的标记无效
- spring-boot - ddl-auto 和 hbm2ddl.auto 有什么区别?
- javascript - 如何查找忽略点或逗号的子字符串的索引