Python Forum
Help with beginner assignment
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with beginner assignment
#1
Hi all, and in advance thanks for helping out the Python rookie :)

For my Programming Class I have to create a program that can search for a substring within a list, and print out all in this case 'countries' containing the substring (if any), else print that no country contains that particular substring. Right now I've managed to get the following result with the code below:

Sorry, but Denmark does not contain the substring 'y'.
The substring 'y' was found in the following country: Germany.
Sorry, but Sweden does not contain the substring 'y'.
The substring 'y' was found in the following country: Norway.
Sorry, but Finland does not contain the substring 'y'.


However, I would very much like a result more like:

The substring 'y' was found in the following countries: Germany, Norway.

or if the substring doesn't exist in the list simply:

Sorry, but the substring does not exist in your list.

...is that possible, if so, how? :)

countries = ['Denmark', 'Germany', 'Sweden', 'Norway', 'Finland']
countries_lower = {country.lower() for country in countries}
substring = 'y'
for country in countries:
    if substring in country.lower():
        print(f"The substring '{substring}' was found in the following country: {country}.")
    else:
        print(f"Sorry, but {country} does not contain the substring '{substring}'.")
Best regards Chris
Reply
#2
countries = ['Denmark', 'Germany', 'Sweden', 'Norway', 'Finland']
substring = 'y'
countries_with_substring = [country for country in countries if substring in country.lower()]

if countries_with_substring:
    str_countries_with_substring = ', '.join(countries_with_substring)
    print(f"The substring '{substring}' was found in the following country: {str_countries_with_substring}.")
else:
    print(f"Sorry, but the '{substring}' does not exist in your list.")
Good luck
nilamo likes this post
Reply
#3
(Feb-23-2021, 03:08 PM)f3b314 Wrote:
countries = ['Denmark', 'Germany', 'Sweden', 'Norway', 'Finland']
substring = 'y'
countries_with_substring = [country for country in countries if substring in country.lower()]

if countries_with_substring:
    str_countries_with_substring = ', '.join(countries_with_substring)
    print(f"The substring '{substring}' was found in the following country: {str_countries_with_substring}.")
else:
    print(f"Sorry, but the '{substring}' does not exist in your list.")
Good luck

Thank you so much! Exactly what I was looking for :)

Best regards Chris
f3b314 and nilamo like this post
Reply
#4
This can also be done using filter(). Personally, I think this is cleaner than a list comprehension, but they're mostly the same.

>>> countries = ['Denmark', 'Germany', 'Sweden', 'Norway', 'Finland']
>>> substring = "y"
>>> list(filter(lambda country: substring in country.lower(), countries))
['Germany', 'Norway']
Reply
#5
computers read one character at a time. left to right, top to bottom.

countries_lower = {country.lower() for country in countries}

This command in line 2 already saved(assigned) values in the countries_lower before line 3 substring = 'y' ran.

Order of operation is important.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020