Reputation: 29
I'm trying to sort the given sentence according to length in a sentence and store the length as key and the word as value. I want to sort this dictionary in a increasing order without using in-built functions.
The code I've tried:
def string_leng(user_string):
leng_dict = {}
user_string = user_string.split(" ")
for word in user_string:
if len(word) in leng_dict:
leng_dict[len(word)].append(word)
else:
leng_dict[len(word)] = [word]
print(leng_dict)
return leng_dict
def main():
input_string = input("Enter sentence")
string_leng(input_string)
if __name__ == "__main__":
main()
The output is as follows:
Enter sentencethis is a test test needs a check
{4: ['this', 'test', 'test'], 2: ['is'], 1: ['a', 'a'], 5: ['needs', 'check']}
The output I'm looking for is :
{5: ['needs', 'check'] , 4: ['this', 'test', 'test'], 2: ['is'], 1: ['a', 'a']}
Upvotes: 0
Views: 655
Reputation: 22360
Firstly a dictionary needs have unique key values so your "looking to split each words" output, i.e.:
{5: ['needs'] , 5: ['check'] , 4: ['this'] , 4: ['test'], 4: ['test'], 2: ['is'], 1: ['a'], 1:['a']}
is not possible.
Regarding your initial desired output, in python3.6+ this is achievable with a slight modification to your current solution since dictionaries maintain insertion order:
def string_leng(user_string):
leng_dict = {}
user_string = user_string.split(" ")
for word in user_string:
if len(word) in leng_dict:
leng_dict[len(word)].append(word)
else:
leng_dict[len(word)] = [word]
res = {}
for key in sorted(leng_dict.keys(), reverse=True):
res[key] = leng_dict[key]
return res
# or equivalently using a dictionary comprehension
# {k : leng_dict[k] for k in sorted(list(leng_dict.keys()), reverse=True)}
def main():
input_string = input("Enter sentence: ")
print(string_leng(input_string))
if __name__ == "__main__":
main()
Example Usage:
Enter sentence: this is a test test needs a check
{5: ['needs', 'check'], 4: ['this', 'test', 'test'], 2: ['is'], 1: ['a', 'a']}
Alternate version that does not use the builtin sorted
or reverse
functions
Feel free to replace the quick_sort
or reverse
functions with different implementations:
def quick_sort(a):
left = []
pivot_list = []
right = []
if len(a) > 1:
pivot = a[0]
for i in a:
if i < pivot:
left.append(i)
elif i > pivot:
right.append(i)
else:
pivot_list.append(i)
left = quick_sort(left)
right = quick_sort(right)
return left + pivot_list + right
return a
def reverse(a):
list_length = len(a)
for i in range(list_length // 2):
a[i], a[list_length - 1 - i] = a[list_length - 1 - i], a[i]
return a
def string_leng(user_string):
leng_dict = {}
user_string = user_string.split(" ")
for word in user_string:
if len(word) in leng_dict:
leng_dict[len(word)].append(word)
else:
leng_dict[len(word)] = [word]
res = {}
for key in reverse(quick_sort(list(leng_dict.keys()))):
res[key] = leng_dict[key]
return res
# or equivalently using a dictionary comprehension
# return {k : leng_dict[k] for k in reverse(quick_sort(list(leng_dict.keys())))}
def main():
input_string = input("Enter sentence: ")
print(string_leng(input_string))
if __name__ == "__main__":
main()
Upvotes: 1