Home > Enterprise >  Get the list of names which satisfy the condition in dictionary from given list
Get the list of names which satisfy the condition in dictionary from given list

Time:01-16

I have a dictionary in which there are three conditions startwith, contains and endwith given below,

dict1 = {'startwith':"Raja, Bina", 'contains':"Tata", "endwith":""}

If user give value in dictionary with comma that means OR i.e "Raja, Bina" = Raja or Bina

now I have a list of name given below

list_of_names = ["Raja Molli Jira", "Bina Tata Birla", "Fira Kiya Too"]

now with above dictionary and list I have to find the name from list which satisfy the conditions given in dictionary, from above example result should be like (need result in list)

required_list = ["Raja Molli Jira", "Bina Tata Birla"]

the name in required_list satisfy the condition given in dictionary which are startwith and contains.

Example 2

if dict1 and list_of_names are :

dict1 = {'startwith':"", 'contains':"Tata, Gola", "endwith":"Too"}
list_of_names = ["Raja Molli Jira", "Bina Tata Birla", "Fira Kiya Too"]

the required_list list will be :

required_list = ["Bina Tata Birla", "Fira Kiya Too"]

the name in required_list satisfy the condition given in dictionary which are contains and endwith

Currently using code

I am able to handle problem if user give single value (without comma) with below code

dict1 = {'startwith':"Raja,Bina", 'contains':"Tata", "endwith":""}
list_of_names = ["Raja Molli Jira", "Bina Tata Birla", 'Fira Kiya Too']
required_list = []
fileopp  = list(dict1.values())

for i in list_of_files:
   #startswith
   if  ((fileopp[0] != "") and (fileopp[1] == "") and (fileopp[2] == "")):
        if i.startswith(fileopp[0]):
               listfilename.append(i)
   #containswith
  elif ((fileopp[0] == "") and (fileopp[1] != '') and (fileopp[2] == "")):
        if i.__contains__(fileopp[1]):
               listfilename.append(i)
   #endiswith
   elif ((fileopp[0] == "") and (fileopp[1] == '') and (fileopp[2] != "")):
        if i.endswith(fileopp[2]):
            listfilename.append(i)
   #startswith and contains with
   elif ((fileopp[0] != "") and (fileopp[1] != "") and (fileopp[2] == "")):
        if (i.startswith(fileopp[0])) and i.__contains__(fileopp[1]):
            listfilename.append(i)
   #startswith and endswith
   elif ((fileopp[0] != "") and (fileopp[1] == "") and (fileopp[2] != "")):
        if (i.startswith(fileopp[0])) and i.endswith(fileopp[2]):
            listfilename.append(i)
   #containswith and endswith
   elif ((fileopp[0] == "") and (fileopp[1] != "") and (fileopp[2] != "")):
        if (i.__contains__(fileopp[1])) and i.endswith(fileopp[2]):
            listfilename.append(i)

Question

If user give Value with comma (Raja,Bina) then above code fails to give result.

Giving the conditions and required result what I want,

First

dict1 = {'startwith':"Fira", 'contains':"", "endwith":"Birla"}
list_of_names = ["Raja Molli Jira", "Bina Tata Birla", "Fira Kiya Too"]

required_list = ["Bina Tata Birla", "Fira Kiya Too"]

Second

dict1 = {'startwith':"Fira, Raja", 'contains':"", "endwith":""}
list_of_names = ["Raja Molli Jira", "Bina Tata Birla", "Fira Kiya Too"]

required_list = ["Raja Molli Jira", "Fira Kiya Too"]

CodePudding user response:

As the two names separated by a comma are in the same string, the code can not work the way you wish as it does not search for the individual names but the whole string "Fira, Raja" which is never given in the list_of_names as there are not even commas. Try making dict1 two-dimensional, so you can give several conditions for the different aspects (such as startswith).

CodePudding user response:

You can try to combine list comprehension with any():

dict1 = {"startwith": "Fira, Raja", "contains": "", "endwith": ""}
list_of_names = ["Raja Molli Jira", "Bina Tata Birla", "Fira Kiya Too"]


def my_filter(dct, lst):
    s = list(v for v in map(str.strip, dct.get("startwith", "").split(",")) if v != "")
    c = list(v for v in map(str.strip, dct.get("contains", "").split(",")) if v != "")
    e = list(v for v in map(str.strip, dct.get("endwith", "").split(",")) if v != "")

    return [
        i
        for i in lst
        if any(i.startswith(x) for x in s)
        or any(x in i for x in c)
        or any(i.endswith(x) for x in e)
    ]


print(my_filter(dict1, list_of_names))

Prints:

['Raja Molli Jira', 'Fira Kiya Too']
  • Related