简体   繁体   English

如何将两个输入相互比较-Python

[英]How to compare two inputs with each other - Python

I am trying to make a program that detects if each letter of an inputted word is in each word of the imputed sentence in order. 我正在尝试制作一个程序,该程序可以按顺序检测输入的单词的每个字母是否在估算语句的每个单词中。 My program works as so. 我的程序是这样工作的。

Word: book
Sentence: look wow lap kill
no
yes
no
yes

It works fine with one word inputted. 只要输入一个单词就可以正常工作。 I am trying to get it to work when I input two or more words. 输入两个或多个单词时,我试图使其正常工作。 My desired output is this. 我想要的输出是这个。

Word: book dogs cowl
Sentence: look wow lap kill

book:
no
yes
no
yes

dogs:
no
yes
no
no

cowl:
no
yes
no
yes

My code that only works with one word inputted. 我的代码只能输入一个单词。 I understand that if i change the my variable named cc it will change the word that is being compared to the sentence. 我知道,如果我更改名为cc的变量,它将更改与句子比较的单词。 For example if cc was changed to 1 the word being compared would become dogs instead of book. 例如,如果将cc更改为1,则被比较的单词将变成狗而不是书。 This allows me to compare only one word at a time, but I want to compare all of the imputed words at the same time. 这使我一次只能比较一个单词,但是我想同时比较所有估算的单词。 I'm not sure how to implement it into a loop. 我不确定如何将其实现为循环。

f = input("Word: ")
gg = f.split(" ")
m = input("Sentence: ")
n = m.split(" ")
y = []
c = 0
cc = 0 #CHANGE THIS AND IT CHANGES THE WORD THAT IS BEING COMPARED TO THE SENTENCE
g = (gg[cc])
l = list(g[c])
while c < len(g):
  if g[c] in n[c]:
    print("yes")
    if g not in y:
      y.append(g)
  else:
    y[:] = [item for item in y if item != g]
    print("no")
  c = c+1

you need to do like this, 你需要这样做

f = input("Word: ")
gg = f.split(" ")
m = input("Sentence: ")
n = m.split(" ")
for i in gg:
    print('%s:'%i)
    for ind,j  in enumerate(i):
        if j in n[ind]:
            print('%s- Yes'%j)
        else:
            print('%s- No'%j)

here is the output, 这是输出,

mohideen@botvfx-dev:~$ python Desktop/run.py 
Word: book dogs cowl
Sentence: look wow lap kill
book:
b - No
o - Yes
o - No
k - Yes
dogs:
d - No
o - Yes
g - No
s - No
cowl:
c - No
o - Yes
w - No
l - Yes
mohideen@botvfx-dev:~$ 

Try this... This should work for you 试试这个...这应该适合你

word = input("Words: ")
words = word.split(" ")
sentence = input("Sentence: ")
sentence = sentence.split(" ")
for a_word in words:
    j=0
    for letter in a_word:
        if letter in sentence[j]:
            print("Yes")
        else:
            print("No")
        j+=1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM