Apologies for the title, couldn't figure out a way to phrase this, if anyone has suggestions I'm happy to edit!
Basically, I have a dictionary and a list. The dictionary:
dict1 = {('red fruit', 'green fruit'): 'apple', ('orange fruit', 'pink fruit'): 'peach', ('five fruit', 'one fruit'): 51, ('ten fruit', 'nine fruit'): 19, ('period', 'exclamation mark'): '.!'}
The list:
list1 = [('alphabet'), ('number'), ('neither')]
What I'm trying to do is to sort the values of the dictionary keys based on the items in the list and return a new dictionary, where the values are now the keys of dict1 and the new keys are the items in the list.
The values for ('alphabet') should be the keys of dict1 when their values are alphabetical, The values for ('number') should be the keys of dict1 when their values are numerical, and the values of ('neither') should be when the values of the keys fail both cases.
For example, my output should be:
{('alphabet'): [('red fruit', 'green fruit'), ('orange fruit', 'pink fruit')], ('number'): [('five fruit', 'one fruit'), ('ten fruit', 'nine fruit')], ('neither'): [('period', 'exclamation mark')]}
I started off by creating a new dictionary with the keys from the list and the values as empty lists, and I want to add values when they fit the requirements stated above (probably by using .isdigit() and .isalpha()) into the empty lists. But I don't really understand how to add the values to the specific keys when they pass the requirements?
I'm a beginner so simple, short codes without importing modules or list comprehension would be ideal, but any guidance will be much appreciated! Thanks!
If I'm understanding correctly, you want to filter out dictionary keys based on the type of their values and then order the keys by their types as stated in a separate list.
To determine the type of strings, you need to be more exact about what counts as 'alphabet' instead of just listing the name of the things you want to define. For example:
def string_type(s):
if s.isalpha():
return 'alphabet'
elif s.isdigit():
return 'number'
else:
return 'neither'
Then, you can proceed to filter out the keys you want and put them into a dictionary by the order you specified in list1
def process(dict1, list1):
output = {k: [] for k in list1}
for key, val in dict1.items():
t = string_type( str(val) )
if t in list1:
output[t].append(key)
return output
Example outputs:
>>> dict1 = {('red fruit', 'green fruit'): 'apple', ('orange fruit', 'pink fruit'): 'peach', ('five fruit', 'one fruit'): 51, ('ten fruit', 'nine fruit'): 19, ('period', 'exclamation mark'): '.!'}
>>> list1 = [('alphabet'), ('number'), ('neither')]
>>> process(dict1, list1)
{'alphabet': [('red fruit', 'green fruit'), ('orange fruit', 'pink fruit')], 'number': [('five fruit', 'one fruit'), ('ten fruit', 'nine fruit')], 'neither': [('period', 'exclamation mark')]}
>>> list2 = [('number'), ('neither'), ('alphabet')]
>>> process(dict1, list2)
{'number': [('five fruit', 'one fruit'), ('ten fruit', 'nine fruit')], 'neither': [('period', 'exclamation mark')], 'alphabet': [('red fruit', 'green fruit'), ('orange fruit', 'pink fruit')]}