问题描述
我有两个列表:
a - 字典
其中包含诸如作为列表的元素
a - dictionary
which contains keywords such as ["impeccable", "obvious", "fantastic", "evident"]
as elements of the list
b - <$ c $(无可挑剔,明显,梦幻般的, c>句子其中包含诸如的句子[我无可挑剔,你太棒了,这很明显,这很明显]
b - sentences
which contains sentences such as ["I am impeccable", "you are fantastic", "that is obvious", "that is evident"]
目标是使用字典
列表作为参考。
The goal is to use the dictionary
list as a reference.
过程如下:
- 为
code>列表,并针对
字典
列表中的每个元素运行它。如果有任何元素存在,然后将该句子吐出一个新列表 - 对
句子中的每个元素重复步骤1
列表。
- Take an element for the
sentences
list and run it against each element in thedictionary
list. If any of the elements exists, then spit out that sentence to a new list - Repeating step 1 for each of the elements in the
sentences
list.
任何帮助将不胜感激。
谢谢。
以下是代码:
sentences = "The book was awesome and envious","splendid job done by those guys", "that was an amazing sale"
dictionary = "awesome","amazing", "fantastic","envious"
##Find Matches
for match in dictionary:
if any(match in value for value in sentences):
print match
推荐答案
现在您已经解决了原来的问题,并修复了向后执行检查的下一个问题,更改了您的所有变量,您可以这样做:
Now that you've fixed the original problem, and fixed the next problem with doing the check backward, and renamed all of your variables, you have this:
for match in dictionary:
if any(match in value for value in sentences):
print match
您的问题是:
嗯,是的,你的匹配
是一个字典项,这就是你正在打印,所以当然这就是你所得到的。
Well, yes, your match
is a dictionary item, and that's what you're printing, so of course that's what you get.
如果你想打印包含字典项的句子,你不能使用,因为整点的功能我们只要返回True,如果任何元素是真的。它不会告诉你哪些 - 其实,如果有不止一个,它会在第一个停止。
If you want to print the sentences that contain the dictionary item, you can't use any
, because the whole point of that function us to just return True if any elements are true. It won't tell you which ones—in fact, if there are more than one, it'll stop at the first one.
如果你不明白功能像任何
和您传递给他们的生成器表达式,您真的不应该将它们用作魔术调用。找出如何将它们写成显式循环,您将能够轻松地回答这些问题。 (请注意,任何
文档直接显示如何编写等效循环。)
If you don't understand functions like any
and the generator expressions you're passing to them, you really shouldn't be using them as magic invocations. Figure out how to write them as explicit loops, and you will be able to answer these problems for yourself easily. (Note that the any
docs directly show you how to write an equivalent loop.)
例如,您现有的代码相当于:
For example, your existing code is equivalent to:
for match in dictionary:
for value in sentences:
if match in value:
print match
break
这样写就应该很明显如何解决它首先,您要打印句子而不是单词,因此打印值
而不是匹配
(再次,它如果您使用有意义的变量名称(如句子
和字词
而不是无意义的名称,如价值
和误导性名称,如 match
...)。其次,您要打印所有匹配的句子,而不仅仅是第一个,所以不要 break
。所以:
Written that way, it should be obvious how to fix it. First, you want to print the sentence instead of the word, so print value
instead of match
(and again, it would really help if you used meaningful variable names like sentence
and word
instead of meaningless names like value
and misleading names like match
…). Second, you want to print all matching sentences, not just the first one, so don't break
. So:
for match in dictionary:
for value in sentences:
if match in value:
print value
如果你回到我的第一个答案,你可能会注意到这是我建议的结构完全相同。
And if you go back to my first answer, you may notice that this is the exact same structure I suggested.
您可以通过使用理解和迭代函数来简化或缩短这一点,但直到您了解简单版本,以及这些理解和迭代功能如何工作。
You can simplify or shorten this by using comprehensions and iterator functions, but not until you understand the simple version, and how those comprehensions and iterator functions work.
这篇关于Python比较两个列表中的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!