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"]
source https://stackoverflow.com/questions/75127926/get-the-list-of-names-which-satisfy-the-condition-in-dictionary-from-given-list
Comments
Post a Comment