Advertisement
mostafasiddiqui

Python Class -9 home work

Oct 16th, 2019
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.23 KB | None | 0 0
  1. print('Adding string character with total in dictionary')
  2. string = 'hi mr partho yesterday was the 9th class and that class environment was really nice thank you'
  3. counts = dict()
  4.  
  5. for line in string:
  6.     words = line.split()
  7.    
  8.     for word in words:
  9.         if word not in counts:
  10.             counts[word] = 1
  11.         else:
  12.             counts[word] += 1
  13.          
  14. print(counts)
  15.  
  16. print('Sort the dictionary by bigger value till 8 nos value')
  17. lst = list()
  18. for key, val in list(counts.items()):
  19.     lst.append((val, key))
  20.  
  21. lst.sort(reverse=True)
  22.  
  23. for key, val in lst[:8]:
  24.     print(key, val)
  25.  
  26. print('Sort the dictionary by smaller value from third smaller value')
  27. lst = list()
  28. for key, val in list(counts.items()):
  29.     lst.append((val, key))
  30.  
  31. lst.sort(reverse=False)
  32.  
  33. for key, val in lst[3:10]:
  34.     print(key, val)
  35.  
  36. print('Calculate avarage by list')
  37. numlist = list()
  38. count=0
  39. while (True):
  40.     inp = input('Enter a number till to enter done: ')
  41.     if inp == 'done': break
  42.     else :
  43.         try:
  44.             value = float(inp)
  45.         except:
  46.             print('Invalid input')
  47.             continue
  48.         numlist.append(value)
  49.         count =count+1
  50.  
  51. average = sum(numlist) / count
  52. print('Average:', average)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement