Python comparing elements in two lists

2024/10/5 19:59:29

I have two lists:

a - dictionary which contains keywords such as ["impeccable", "obvious", "fantastic", "evident"] as elements of the list

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.

The process is as follows:

  1. Take an element for the sentences list and run it against each element in the dictionary list. If any of the elements exists, then spit out that sentence to a new list
  2. Repeating step 1 for each of the elements in the sentences list.

Any help would be much appreciated.

Thanks.

Below is the code:

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
Answer

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

And your problem with it is:

The way I have the code written i can get the dictionary items but instead i want to print the sentences.

Well, yes, your match is a dictionary item, and that's what you're printing, so of course that's what you get.

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 matchbreak

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.

https://en.xdnf.cn/q/120721.html

Related Q&A

Remove all keys that have values of N/A, -, or empty strings [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 2…

Sorting algorithms more efficient than bubble sort [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 7 years ago.Improve…

How can I return the odd numbers of a list, using only recursion in Python? [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable…

TypeError: int object is not iterable; Python 2.7

Here is my code:def numbers_in_lists(string):num = int(string)l = list(num)return lstring = 543987When i run it:print numbers_in_lists(string)I have the following error:l = list(num) TypeError: int obj…

Python: Are `hash` values for built-in numeric types, strings standardised?

I came to this question while pondering about the ordering of set, frozenset and dict. Python doesnt guarantee any ordering, and any ordering is coupled to the hash value at some level. But is the hash…

How to create a sample django project?

This doesnt work for me.$ python django-admin.py startproject myprojectI am running a ubuntu virtual m/c on my windows system. By default ubuntu 12.04 comes with python 2.7.3 so I am using that only I …

Why elif statement instead of if statement? [duplicate]

This question already has answers here:Difference between multiple ifs and elifs?(9 answers)Why we use if, else if instead of multiple if block if the body is a return statement(13 answers)Closed 7 ye…

How to understand regular expression with python?

Im new with python. Could anybody help me on how I can create a regular expression given a list of strings like this:test_string = "pero pero CC tan tan RGantigua antiguo AQ0FS0que que CS segn se…

How do I reverse words in a string with Python [duplicate]

This question already has answers here:Reversing words in a Python string (including punctuation)(5 answers)Closed last month.I am trying to reverse words of a string, but having difficulty, any assist…

Reading input files and writing into output files - Python

I have an input file (input.txt) with the following information:Number of students (first line) Number of test scores (second line) list of student names and scoresSo the text file looks something like…