Skip to main content

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

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

Popular posts from this blog

How to show number of registered users in Laravel based on usertype?

i'm trying to display data from the database in the admin dashboard i used this: <?php use Illuminate\Support\Facades\DB; $users = DB::table('users')->count(); echo $users; ?> and i have successfully get the correct data from the database but what if i want to display a specific data for example in this user table there is "usertype" that specify if the user is normal user or admin i want to user the same code above but to display a specific usertype i tried this: <?php use Illuminate\Support\Facades\DB; $users = DB::table('users')->count()->WHERE usertype =admin; echo $users; ?> but it didn't work, what am i doing wrong? source https://stackoverflow.com/questions/68199726/how-to-show-number-of-registered-users-in-laravel-based-on-usertype

Why is my reports service not connecting?

I am trying to pull some data from a Postgres database using Node.js and node-postures but I can't figure out why my service isn't connecting. my routes/index.js file: const express = require('express'); const router = express.Router(); const ordersCountController = require('../controllers/ordersCountController'); const ordersController = require('../controllers/ordersController'); const weeklyReportsController = require('../controllers/weeklyReportsController'); router.get('/orders_count', ordersCountController); router.get('/orders', ordersController); router.get('/weekly_reports', weeklyReportsController); module.exports = router; My controllers/weeklyReportsController.js file: const weeklyReportsService = require('../services/weeklyReportsService'); const weeklyReportsController = async (req, res) => { try { const data = await weeklyReportsService; res.json({data}) console...

How to split a rinex file if I need 24 hours data

Trying to divide rinex file using the command gfzrnx but getting this error. While doing that getting this error msg 'gfzrnx' is not recognized as an internal or external command Trying to split rinex file using the command gfzrnx. also install'gfzrnx'. my doubt is I need to run this program in 'gfzrnx' or in 'cmdprompt'. I am expecting a rinex file with 24 hrs or 1 day data.I Have 48 hrs data in RINEX format. Please help me to solve this issue. source https://stackoverflow.com/questions/75385367/how-to-split-a-rinex-file-if-i-need-24-hours-data