+ 2
Does anyone know how to iterate over items in a list with items in a dictionary value in python?
If I had a list = [0, 1, 2, 3, 4, 5] and i wanted to search through a dictionary(dict = {'a': [1, 2], 'b': [3, 5], 'c': [4, 6]} for either a key or its value (which happens to be a list) and wanted to go item by item in the list and search both the keys and values in the dictionary for the item in the list, how would I go about that?
9 Respostas
+ 4
@Ava Nicole - I thought I read you were trying to explore recursion, so no worries. Examples don't always have to be ideal cases.
In my mind, dictionaries are just a way to give names to things [otherwise, this sentence would just be a string of integers instead of words--we won't need another Shakespeare to show why that's a terrible idea]. Since dictionaries can use "words" instead of an index (or offset from 0, the first element) to identify values, it no longer matters what order the entries are in.
visph and richard show ways of iterating a dictionary, here's another one:
dict = {"a": 1, "b": 2}
for key in dict:
print(key) # a, b...or b, a
print(dict[key]) # order of the keys you get
The final output will be one of these columns:
a b
1 2
b a
2 1
+ 3
At first blush...I've seen recursion when order / detail is important (chaining dependent results just being a form of ordering)...but dictionaries are unordered...+ you aren't honing in on additional detail or changing sequences by going deeper.
That said...at least one stacking logic works: boolean. I'm not sure I've ever seen this, but you can recursively stack an "a in [x]" for the straight list this way (thinking about the dictionary is distracting; my brain wants to change the problem definition):
def myRecursiveIn(mySearch, myList):
if len(myList) == 1:
return mySearch == myList[0]
# otherwise...
return (mySearch == myList[0]) or myRecursiveIn(mySearch, myList[1:])
I'll go test this to ensure it works. I'll note it either way, since it may still yield inspiration.
+ 3
Well...it works:
https://code.sololearn.com/c9xsVsQilIbL/?ref=app
Somewhat fascinating to me: the algorithm doesn't short-circuit, but Python does (stopping the recursion as soon as it hits a True). I added a print to show this.
+ 2
# No need of recursive function... just nested loops:
list = [0, 1, 2, 3, 4, 5]
dict = {'a': [1, 2], 'b': [3, 5], 'c': [4, 6]}
def search(search,target):
result = []
# search 'search' list items in 'target' dict
for item in search:
for key, value in dict.items():
print('list item: {}, dict key: {}, dict value: {}'.format(item,key,value))
if item in value:
index = value.index(item)
print('> item {} in value {} at key {}, index {}'.format(item,value,key,index))
result.append({'found':item,'key':key,'index':index})
print()
return result
match = search(list,dict)
#print('result : {}'.format(match))
print('result:')
for m in match:
print('> found {} in "{}" key at index {}'.format(m['found'],m['key'],m['index']))
+ 1
I'm a little confused how I could write a recursive function for this?
+ 1
@Kirk Schafer thanks for trying that haha. I'm still trying to understand it but I think I got all confused about dictionaries now
+ 1
Yeah I got kinda lost on dictionaries when I was trying to learn how to iterate over them
0
dict.items() to get the tuple of both keys anfmd values, all inside a recursive function?
0
my_list = [0, 1, 2, 3, 4, 5]
my_dictionary = {'a': [1, 2],
'b': [45, 5],
'c': [4, 6]}
# check if item in your list is a dictionary key
for item in my_list:
print(item ," is key ",item in my_dictionary.keys())
# check if item in your list is a dictionary value in a dictionary
# where values are all in lists
flattened_list = sum(my_dictionary.values(),[])
for item in my_list:
print(item, "is value ",item in flattened_list)