Simple way to find strings in sets? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Simple way to find strings in sets?

Is there a simple way to find partially matching strings or matching substrings between user input and a set? Or even between 2 sets? I've been messing around with this for a few days and I feel like I'm missing something. Below I have 2 examples of what is not working so far: #ii would be input, but for sake of example it's 'farmer'. Roots = {'farm', 'grow', 'work'} ii = 'farmer' print(ii in Roots) #OR ii = {ii} print(str in Roots & ii) Output: False False

7th Feb 2024, 10:47 PM
Aza
Aza - avatar
4 Answers
+ 1
Well, you may use a set comprehension to get this done. Maybe like this: user_input = 'farmer' roots = {'farm', 'grow', 'work'} matching_strings = {s for s in roots if s in user_input} # set comprehension print(matching_strings) And the output you get here is: {'farm'} Hope this helps you. ☺
8th Feb 2024, 2:06 AM
I-M-J
I-M-J - avatar
+ 1
Aza , A hint: Even though set is unordered, it's iterable. Imagine a bag of marbles that you blindly reach your hand into and remove one by one. You can't guarantee the order, but you're guaranteed to get them all. So you can use a for loop on Roots and do your comparisons inside the loop. Also, Roots should be roots, since it's not a class.
8th Feb 2024, 2:01 AM
Rain
Rain - avatar
+ 1
While others give the answer, I would give you an explanation why your code gives False, so you can learn from the error and prevent to make a same mistake in the future. I think you already know about dict. Set is similar to dict, but set only contains the "keys", not a key-value pair. So each element inside a set is unique. Alternatively, set is similar to list, but the count of every element must be 1. Set, dict and list are all iterable, meaning you can use a for loop / while loop to work with them. In your first example, Roots has 3 "keys" and ii has a "key" (farmer). Because the key of ii is not inside the Roots set, it returns False. Thinking like a list, the string 'farmer' is not inside the "list" of roots. ('farmer' is not a member of "list" Roots) In your second example, you turn ii from a string into a set at the beginning. For readability, let me modify your print statement. print(str in (Roots & ii)) In the <Roots & ii> part, since there is no common element in two sets, it returns an empty set {}. Then it turns out to be: print(str in {}) str is a class, and since the set is empty, it returns False. (You will learn more about Class when reaching OOP). But I think you are asking "Is a string 'farmer' or 'farm' inside a set", and unfortunately the answer is False. It is because Roots & ii returns an empty set, neither string is inside it.
8th Feb 2024, 3:36 AM
Wong Hei Ming
Wong Hei Ming - avatar
+ 1
Perfect!! Thank you all! Thanks for the tip on classes too, I didn't know that! I also liked the explanation of why those results were False
8th Feb 2024, 3:37 AM
Aza
Aza - avatar