python - Truly finding the "next" answer in list of dictionaries -
i've run situation need find next index of list of dictionaries after point.
so far i've had find uniquely identified elements name, have used next(x x in list if x[prop] == search_term)
. need not find unique element, find specific element after specified point in list (a point may know number or may have find well, finding sec "red" element after first "red" element).
not finding built-in function in documentation , forums , stackoverflow, decided should build own function. first attempt:
def search_dict(my_dict, current_position, search_prop, search_term): try: homecoming next(index (index, d) in enumerate(my_dict[current_position:]) if search_prop in d if d[search_prop] == search_term) + current_position except stopiteration: homecoming none
i exchanged my_dict
my_dict[current_position:]
pass subset of code, , add together current position onto value homecoming index of whole list. here test list:
a = [{"name":"a","color":"red"},{"name":"b","color":"blue"},{"name":"c","color":"red"},{"name":"d","color":"blue"},{"name":"e","color":"green"},{"name":"f","color":"green"}]
and test function:
print(search_dict(a, search_dict(a, 0, "color", "blue"), "color", "red")) #this should find first "blue" element @ index 1 , homecoming next "red" @ 2 print(search_dict(a, search_dict(a, 0, "color", "red"), "color", "red")) #this should find first "red" element @ index 0 , homecoming next "red" @ index 2 print(search_dict(a, search_dict(a, 0, "name", "d"), "color", "red")) #this should find "d" element , go end of list returning 'none' print(search_dict(a, 0, "color", "blue")) #should homecoming 1 print(search_dict(a, 0, "color", "red")) #should homecoming 0
actual output:
2 #corrent 0 #incorrect none #correct 1 #correct 0 #correct
they right except sec one; returns 0 first reddish instead of returning 2 sec red. tried multiple things prepare this, , realized wanted code not output none
@ end of list "wrap" around , out first element. rewrote function this:
def search_dict(my_dict, current_position, search_prop, search_term): new_dict = [x (x, d) in enumerate(my_dict) if search_prop in d if d[search_prop] == search_term] try: homecoming next(x x in new_dict if x > current_position) except stopiteration: homecoming next(x x in new_dict)
i first had compile list of index points pertaining search criteria had homecoming next index greater current position. output:
2 #correct 0 #incorrect 0 #correct 1 #correct 2 #incorrect <- fixed changing operator if x >= current_position
sadly 1 problem 2 test, , 1 important. trying find sec or 3rd 'x'
after 'x'
.
i added test, looking sec green, should @ index 5. instead returns 4.
partial solve: numbers homecoming correctly if alter operator >
, add together empty element front end of list.
search_dict(a, 0, "color", "red")
returns 0, sec test calling same function.
my_dict[0:] == my_dict
you perhaps need call
print(search_dict(a, search_dict(a, 0, "color", "red") + 1, "color", "red"))
either or function needs utilize current_position + 1
python list search dictionary next
No comments:
Post a Comment