How to correctly convert from a term(str) to a list and then a dictionary?

Question:

My task:

Your company blogs. You need to implement the find_articles function
to search for articles on this blog. There is a list articles_dict,
which contains the description of blog articles. Each element of this
list is a dictionary with the following keys:

  • surnames of authors – key ‘author’
  • title of the article – key ‘title’
  • year of publication – key ‘year’.

Implement the find_articles function,

The key parameter of the function defines the combination of letters
to search for. For example, with key="Python", the function looks to
see if there are articles in the articles_dict list with this
combination of letters in the title or author names. If such list
items were found, a new list should be returned from dictionaries
containing the authors’ surnames, title and year of publication of all
such articles.

The second key parameter of the letter_case function determines
whether the search should be case-sensitive. By default, it is False
and case does not matter, that is, searching in the text "Python" and
"python" is the same. Otherwise, you need to look for a complete
match.

My code:

articles_dict = [
    {
        "title": "Endless ocean waters.",
        "author": "Jhon Stark",
        "year": 2019,
    },
    {
        "title": "Oceans of other planets are full of silver",
        "author": "Artur Clark",
        "year": 2020,
    },
    {
        "title": "An ocean that cannot be crossed.",
        "author": "Silver Name",
        "year": 2021,
    },
    {
        "title": "The ocean that you love.",
        "author": "Golden Gun",
        "year": 2021,
    },
]


# print(articles_dict[0]["title"])

def find_articles(key, letter_case=False):
    lst = []
    for l in articles_dict:
        for k, v in l.items():
            lst.append(f'{k} {v}n')
            result = "".join(lst)
    print(result)

First of all, tell me, in your opinion, is my logic correct in building the code algorithm to achieve this task?

Secondly, can you tell me where to use letter_case? I think that a while loop is needed, but where should it be written?

Thirdly, I know that the main methods of finding a substring in a line are strings, that’s why I made a string here, but how can I check if there is, for example, key = "Python" in a given line in the task. And this is how I understood that it is necessary to take into account the case, I suspect that it is necessary use .lower() and .upper().

Fourthly, am I not complicating everything again? After I check whether there is key = "Python" or something else in this text, should I correctly convert then from a string to a list and then to a dictionary? Should I first use the split() method or list( ) and then dict()?

Thank you! Can you correct my logic and point me in the right direction!

Asked By: Alduin

||

Answers:

No, your code isn’t correct since it just prints everything and ignores your parameters completely.

You need lines like if key in l['author'] or key in l['title'], as the instructions say looks to see if … this combination of letters in the title or author names

where to use letter_case

You can use re module to check case-insensitive matches in strings. Otherwise, simply key in value.lower() could work; you don’t also need upper().

that’s why I made a string here

The question asked you to return the whole object of the matching articles (a list of dicts), not just a string. So, you’d want lst.append(l), but only if the above conditions are true. And you want return lst, rather than print within the function.

Answered By: OneCricketeer
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.