Python 3.x 我有一个关于我的程序的问题,它读取用户输入的两个句子,然后以各种方式进行比较

Python 3.x 我有一个关于我的程序的问题,它读取用户输入的两个句子,然后以各种方式进行比较,python-3.x,Python 3.x,我是Python的新手,我正在尝试编写一个程序,读取用户提供的两个句子,然后通过以下方式进行比较: I.它必须显示包含在两个句子中的所有独特单词的列表 二,。它必须显示出现在两个句子中的单词列表 三、 它必须显示出现在第一句而不是第二句中的单词列表 四、 它必须显示出现在第二句而不是第一句中的单词列表 V.它必须显示出现在第一句或第二句中的单词列表,但不能同时出现在两句中 我试图在这个网站上搜索我的问题,但我找不到任何能准确描述我问题的东西。我试着阅读我的书(学习Python-5th Ed),甚

我是Python的新手,我正在尝试编写一个程序,读取用户提供的两个句子,然后通过以下方式进行比较:

I.它必须显示包含在两个句子中的所有独特单词的列表

二,。它必须显示出现在两个句子中的单词列表

三、 它必须显示出现在第一句而不是第二句中的单词列表

四、 它必须显示出现在第二句而不是第一句中的单词列表

V.它必须显示出现在第一句或第二句中的单词列表,但不能同时出现在两句中

我试图在这个网站上搜索我的问题,但我找不到任何能准确描述我问题的东西。我试着阅读我的书(学习Python-5th Ed),甚至在网上搜索如何让我的程序正常运行的指针

这是我的代码,我提前道歉,因为我知道这不是处理这类程序的最有效方法;但是我对Python是全新的,我想不出更好的方法:

def sentenceDisplay():

句子显示

def SENT1和2显示()

Sent1和2显示()

def diffOfUnion():

Diffounion()

def symmDiff():

symmDiff()

我知道使用集合运算是我需要做的,但是我的函数(#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中包含的单词:句子,随机

二,。来自以下任一词的所有单词:A,random,句子,Some,other,random,句子

三、 仅包含在第1句中的单词:A

四、 仅第2句中的单词:Some,other


V.句子#1或#2中包含的单词,但不能同时包含:A、Some、other

谢谢。我修改了我的代码以更接近您的代码,但我仍然希望程序要求用户输入两个句子——我设法让它工作并以列表的形式显示结果(通过编辑我的打印语句)。不,事实上,这是一位同事的请求,想看看我是否有能力编写这样一个程序。再次感谢您的帮助。是的,我省略了输入部分,这样示例就有意义了,因为您已经有了它。很高兴我能帮忙。祝你工作顺利。:)
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))
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))
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))
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)}")