Python How To Assign 2 Variables in One Part(?)

Question:

Im sorry ı couldn’t write decent title. But let me explain.

a list that holds ten variable
list[0] = tshirt
but if user search t-shirt in that list, program should accept it as tshirt.
What Im asking is, can I say

list[0] = "tshirt" or "t-shirt"

if not, must I do that manually or is there any way to do this?

thanks.

Edit: Can any mod fix my title so people can understand the problem and find answer below

Asked By: rookie

||

Answers:

You can either check for two values using the in operator:

list[0] in ("tshirt", "t-shirt")

or check for "tshirt" only but replace all occurrences of - with empty spaces, so that both "tshirt" and "t-shirt" (changed to "tshirt") actually match.

list[0].replace('-', '') = "tshirt" 

Check the Python demo.

Answered By: lemon

I did not fully understand but, you want to ignore the "-" character?

if list[0].replace('-','')=="tshirt":
    True

That would be very bad use. You should better explain what you need.

Answered By: madeinevo

I remember , I faced this issue too in one of my projects.There is a python library called fuzzywuzzy which tells if two words are similar or not based on the similarity score.Here’s an example stuff.

from fuzzywuzzy import fuzz
temp = "t-shirt"
if fuzz.token_sort_ratio("tshirt", temp) > 70:
    print("Found")
if fuzz.token_sort_ratio("TSHIRT", temp) > 70:
    print("Found")
if fuzz.token_sort_ratio("T--SHIRT", temp) > 70:
    print("Found")

It Outputs FOUND for all three statements.

This library provides us a solution to a problem which most of us might have faced.

Answered By: Aditya Singh
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.