Home > Back-end >  Find items containing the asked string in a dict of dicts python
Find items containing the asked string in a dict of dicts python

Time:11-02

A collection of books and movies are already parsed and read into a dict of dicts. The program asks a user to enter a query string to search against multiple fields in the collection: title, author, and publisher. The search should perform partial string matching and be case insensitive. All matching books should be displayed.

Suppose there are 3 books and 3 movies.


library_collections = {'books': {17104: {'Title': 'A River', 'Author': 'Elisha Mitchell', 'Publisher': 'FPG Publishing', 'Pages': '345', 'Year': '2014', 'Copies': 2, 'Available': 2, 'ID': 17104}, 37115: {'Title': 'Aim High', 'Author': 'George Tayloe Winston', 'Publisher': 'Manning Hall Press', 'Pages': '663', 'Year': '2014', 'Copies': 5, 'Available': 5, 'ID': 37115}}, 'movies': {27002: {'Title': 'Adventures of Argo', 'Director': 'Jonathan Trumbull Jr', 'Length': '102', 'Genre': 'Drama', 'Year': '2014', 'Copies': 3, 'Available': 3, 'ID': 27002}, 17003: {'Title': 'African Queen', 'Director': 'Frederick Muhlenberg', 'Length': '98', 'Genre': 'Drama', 'Year': '2011', 'Copies': 2, 'Available': 2, 'ID': 17003}, 57004: {'Title': 'Alice May', 'Director': 'Jonathan Dayton', 'Length': '117', 'Genre': 'Drama', 'Year': '2014', 'Copies': 2, 'Available': 2, 'ID': 57004}}}


query = input('Please enter a query string to search: ')
for key, value in library_collections.items():
    if query.lower() in value:
        print(key,value)
        print()

It prints nothing to the console, can anyone help?

CodePudding user response:

Well, you make a mistake on your for loop.

Now, the key is books, and the value is {17104: {'Title': 'A River', 'Author': 'Elisha Mitchell', 'Publisher': 'FPG Publishing', 'Pages': '345', 'Year': '2014', 'Copies': 2, 'Available': 2, 'ID': 17104}, 37115: {'Title': 'Aim High', 'Author': 'George Tayloe Winston', 'Publisher': 'Manning Hall Press', 'Pages': '663', 'Year': '2014', 'Copies': 5, 'Available': 5, 'ID': 37115}}

So what you want to do is to add few more for loop. Hope you won't mind my awful algorithm.

Here is my solution:

library_collections = {'books': \
                       {17104: {'Title': 'A River', 'Author': 'Elisha Mitchell', 'Publisher': 'FPG Publishing', 'Pages': '345', 'Year': '2014', 'Copies': 2, 'Available': 2, 'ID': 17104}, \
                        37115: {'Title': 'Aim High', 'Author': 'George Tayloe Winston', 'Publisher': 'Manning Hall Press', 'Pages': '663', 'Year': '2014', 'Copies': 5, 'Available': 5, 'ID': 37115}}, \
                       'movies': \
                       {27002: {'Title': 'Adventures of Argo', 'Director': 'Jonathan Trumbull Jr', 'Length': '102', 'Genre': 'Drama', 'Year': '2014', 'Copies': 3, 'Available': 3, 'ID': 27002}, \
                        17003: {'Title': 'African Queen', 'Director': 'Frederick Muhlenberg', 'Length': '98', 'Genre': 'Drama', 'Year': '2011', 'Copies': 2, 'Available': 2, 'ID': 17003}, \
                        57004: {'Title': 'Alice May', 'Director': 'Jonathan Dayton', 'Length': '117', 'Genre': 'Drama', 'Year': '2014', 'Copies': 2, 'Available': 2, 'ID': 57004}}}

query = input('Please enter a query string to search: ')
for v in library_collections:
    for ids,info in library_collections[v].items():
        for index,value in info.items():
            if type(value)==str and query.lower() in value.lower():
                print(ids,info,end='\n\n')

CodePudding user response:

This shows how you might navigate the nested dictionaries. Your could input for example 'river' or 'elisha'. You could simply extend it to deal with movies.

query = input('Please enter a query string to search: ').lower()
for key, value in library_collections.items():
    for x, y in value.items():
        if 'books' in key:
            if (query in y['Title'].lower()) or (query in y['Author'].lower()) or (query in y['Publisher'].lower()):
                print(y)
        
  • Related