Reputation: 387
I would appreciate it if somebody could help me solve the following exercise.
Write a function that takes a string containing words separated by spaces and creates a dictionary where the keys correspond to the last characters of the words of the string, whilst the associated values correspond to the lists having the words ending in that character.
Furthermore, the lists must be lexicographically sorted
Example
string = "This will be an amazing experience"
Should return the following dictionary
{'s':['This'], 'l':['will'], 'e':['be', 'experience'], 'n':['an'], 'g':['amazing']}
I have an idea of how the exercise should be solved. However, given that i am still a beginner in Python, i struggle.
The first thing i did is create an empty dictionary. Secondly, I converted the string of words into a list using the split method. Afterwards, i created another list that takes the last character of the words of the first list of strings and puts it into the new list.
I used dict in order to put the elements of the second list as keys in my dictionary which works quite well. The problem is i cannot put the list of corresponding words as values.
Here is my code:
def dictionary (string):
mydict = {}
mylist = string.split()
mylist2 = []
for word in mylist:
mylist2.append(word[-1])
mydict = dict((el,(word[-1] for word[-1] in mylist)) for el in mylist2)
return mydict
Upvotes: 1
Views: 452
Reputation: 2545
For the lexicographical sorting part, you'll need to remember to sort the lists once you've generated them:
my_string = "This will be an amazing experience"
def make_dict(string):
my_dict = {}
for word in string.split():
my_dict.setdefault(word[-1], []).append(word)
for l in my_dict.values():
l.sort()
return my_dict
print(make_dict(my_string))
Another possibility is to build heaps, rather than lists, and then quickly generate sorted lists from those heaps. Depending on your exact use-case, your mileage may vary.
Upvotes: 1
Reputation: 3176
st = "This will be an amazing experience"
def func(st):
d={}
for v in st.split(' '):
if v[-1] in d:
d[v[-1]].append(v)
else:
d[v[-1]]=[v]
return d
print(func(st))
Upvotes: 0
Reputation: 20500
You can use a collections.defaultdict to make a dictionary with an empty list with a default value. Then while iterating over the list of words, you can extract the last character as a key and add the word to the value list for that key
import collections
def dictionary(string):
#Dictionary with default value as empty list
mydict = collections.defaultdict(list)
#Iterate over list of words
for word in string.split():
#Take last character and add word to the value list for that character
last_char = word[-1]
mydict[last_char].append(word)
return dict(mydict)
print(dictionary("This will be an amazing experience"))
The output will be
{'s': ['This'], 'l': ['will'], 'e': ['be', 'experience'], 'n': ['an'], 'g': ['amazing']}
Upvotes: 1
Reputation: 522567
You don't need two lists, the algorithm is pretty simple:
result = {}
for word in string.split():
key = word[-1]
if key not in result:
result[key] = []
result[key].append(word)
There are ways to shorten that using dict.setdefault
or defaultdict
, but this is the basic idea.
Upvotes: 1