Python Forum
filtering a list of dictionary as per given criteria
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
filtering a list of dictionary as per given criteria
#1
i have metadata and api_request as below.

metadata = [

    {"path": "d/scripts", "name": "../"},
    {"path": "d/scripts", "name": "../"},
    {"path": "d/scripts", "name": "unn_details_in.cfg"},
    {"path": "d/scripts", "name": "unn_details_out.cfg"},
    {"path": "d/scripts", "name": "unn_details_in_v1.cfg"},
    {"path": "d/scripts", "name": "unn_details_out_v1.cfg"},
    {"path": "c/scripts", "name": "./"},
    {"path": "c/scripts", "name": "../"}


]

api_request = {"response_filter": [
    {"path": {"value": "d/scripts"}, "name": {"value": "[a-z_]*details_in[a-z1-2_]*.cfg$", "operator": "match"}},
    {"path": {"value": "d/scripts"}, "name": {"value": "[a-z_]*details_out[a-z1-2_]*.cfg$", "operator": "match"}}
]}

final_list=[]
for details in api_request["response_filter"]:
    for key,value in details.items():
        final_list.extend(
            data
            for data in metadata
            if OPERATION_MAPPER[value.get("operator","eq")](value.get("value"),data[key])


         and data not in final_list)

print(final_list)
i am supposed to iterate api_request["response_filter"] and what ever is the value for path(in this case d/scripts),i need to filter

only that data and in those filtered data , i need to ensure that name should of the mentioned regex.

Requirement:detail_in and detail_out should be present in a given metadata

if the filtered data has both detail_in and detail_out present in name for a given metadata...i should not raise any error.if either

detail_in or detail_out is absent ,i need to raise error and mentioned the error like "detail in not present" or "detailout not present "

i did the following way,but i am not able to proceed further


for the above example of metadata ,there should be no error raised

suppose the metadata is as below

metadata = [

    {"path": "d/scripts", "name": "../"},
    {"path": "d/scripts", "name": "../"},
    {"path": "d/scripts", "name": "unn_details_in.cfg"},
    {"path": "d/scripts", "name": "unn_details_in_v1.cfg"},
    {"path": "c/scripts", "name": "./"},
    {"path": "c/scripts", "name": "../"}


]
we should get the message as details_out absent

waiting for a response.Thank you
Reply
#2
what have you tried so far? Please show your code attempt, working or not.
Reply
#3
(Dec-22-2023, 08:12 PM)Larz60+ Wrote: what have you tried so far? Please show your code attempt, working or not.

I have pasted my above code above in the initial message itself.please have a look.
It is not working as expected
Reply
#4
What are detail_in and detail_out?
« We can solve any problem by introducing an extra level of indirection »
Reply
#5
(Dec-23-2023, 07:45 AM)Gribouillis Wrote: What are detail_in and detail_out?
detail_in and detail_out is the substring in the value of key called "name" in the given metadata as shown below
[
 
    {"path": "d/scripts", "name": "../"},
    {"path": "d/scripts", "name": "../"},
    {"path": "d/scripts", "name": "unn_details_in.cfg"},
    {"path": "d/scripts", "name": "unn_details_in_v1.cfg"},
    {"path": "c/scripts", "name": "./"},
    {"path": "c/scripts", "name": "../"}
 
 
]
i need to check if a given list of metadata contains detail_in and detail_out present in the name field.If both the detail_in and detail_out are present in a list of metadata,i should not raise error.If either of them is present and one of them is absent .i need to know which is absent in the error message
Reply
#6
(Dec-23-2023, 08:32 AM)jss Wrote: i need to check if a given list of metadata contains detail_in and detail_out present in the name field
You can use the in operator with strings. Note that 'detail_in' is different from 'details_in'
>>> 'details_in' in "unn_details_in_v1.cfg"
True
>>> 'details_in' in "unn_details_out_v1.cfg"
False
>>> 
« We can solve any problem by introducing an extra level of indirection »
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Dictionary in a list bashage 2 564 Dec-27-2023, 04:04 PM
Last Post: deanhystad
  Sort a list of dictionaries by the only dictionary key Calab 1 498 Oct-27-2023, 03:03 PM
Last Post: buran
  How to add list to dictionary? Kull_Khan 3 1,014 Apr-04-2023, 08:35 AM
Last Post: ClaytonMorrison
  check if element is in a list in a dictionary value ambrozote 4 1,989 May-11-2022, 06:05 PM
Last Post: deanhystad
  Dictionary from a list failed, help needed leoahum 7 1,976 Apr-28-2022, 06:59 AM
Last Post: buran
  how to assign items from a list to a dictionary CompleteNewb 3 1,594 Mar-19-2022, 01:25 AM
Last Post: deanhystad
  Python, how to manage multiple data in list or dictionary with calculations and FIFO Mikeardy 8 2,619 Dec-31-2021, 07:47 AM
Last Post: Mikeardy
  Class-Aggregation and creating a list/dictionary IoannisDem 1 1,927 Oct-03-2021, 05:16 PM
Last Post: Yoriz
  Python dictionary with values as list to CSV Sritej26 4 3,028 Mar-27-2021, 05:53 PM
Last Post: Sritej26
  convert List with dictionaries to a single dictionary iamaghost 3 2,879 Jan-22-2021, 03:56 PM
Last Post: iamaghost

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020