Question

In: Computer Science

Write a Python program to count occurrences of items (and retrieve the most 3 or least...

  1. Write a Python program to count occurrences of items (and retrieve the most 3 or least 3 words).
  2. Write a Python program to sort a dictionary by keys or values in ascending or descending order by 2 methods.

Solutions

Expert Solution

Program 1:

l=[1,1,1,1,1,2,2,2,2,3,3,3,4,5,6]
#In the above list the most three occurrences are 1,2,3 and least three occurrences are 4,5,6
d=dict()
for i in l:
d[i]=0
  
for i in l:#for each element in list
d[i]+=1#increment count value in dictionary
  
#sort the dictionary by value and get the results
l=sorted(d.items())#obtain the list of tuples
l.sort(key=lambda i:i[1])#sort the list based on second field in ascending order
print("The least three occuring elements are :")
for i in range(3):
print(l[i][0])

l.sort(key=lambda i:i[1],reverse=True)#sort the list based on second field in descending order
print("The most three occuring elements are :")
for i in range(3):
print(l[i][0])

Program 2:

d={1:"word1",3:"temp",2:"value3"}

#sorting by key
print("Sorting by Key in ascending order")
temp=(sorted(d.items()))
for i in temp:
print(i[0],"=>",i[1])#print after Sorting

print("Sorting by Value in asscending order")
#Sorting by value
l=[]
for k,v in d.items():
l.append((k,v))#append the tuples to list

l.sort(key=lambda i:i[1])#sort using second element of tuple which is value of dict
for i in l:
print(i[0],"=>",i[1])#print after Sorting


Related Solutions

In this program: ================================================================== /* Program to count number of occurrences of a given string in...
In this program: ================================================================== /* Program to count number of occurrences of a given string in original string*/ #include <iostream> #include <cstring> #include <stdio.h> #include <iostream> using namespace std; int main() { const int SIZE = 40; char str[SIZE]; char str1[SIZE]; char searchString[SIZE]; int n; int l1, l2; int count = 0; printf("Enter a sentence: \n"); fgets(str,SIZE,stdin); printf("Enter a search word: \n"); fgets(searchString,SIZE,stdin); if (str1[strlen(str1) - 1] == '\n') { str1[strlen(str1)-1] = '\0'; } if (str[strlen(str) - 1] == '\n')...
In python, Modify your mapper to count the number of occurrences of each character (including punctuation...
In python, Modify your mapper to count the number of occurrences of each character (including punctuation marks) in the file. Practice the given tasks in Jupyter notebook first before running them on AWS. If your program fails, check out stderr log file for information about the error. import sys sys.path.append('.') for line in sys.stdin:    line = line.strip() #trim spaces from beginning and end    keys = line.split() #split line by space    for key in keys:        value...
Given a string, such as x = ‘itm330’, write a Python program to count the number...
Given a string, such as x = ‘itm330’, write a Python program to count the number of digits and the number of letters in it. For example, in ‘itm330’, there are 3 letters and 3 digits. Hint: Very similar to page 11 on the slides. To check if a character c is a digit, use c.isdigit(). If c is a digit, c.isdigit() will be a True.
for Python 3 Write a python program that Creates a list that has these values in...
for Python 3 Write a python program that Creates a list that has these values in this order, 'Python', 'JavaScript', and 'PHP' Define a displayMyClasses function that sorts the list and then displays each item on the list, one per line, in sorted order. Also, number the displayed list as shown in the "Running a Sample Program" shown below. Define a guessNext function that selects a random element from the list and returns it to the call of the function....
A customer comes into a grocery store and buys 8 items. Write a PYTHON program that...
A customer comes into a grocery store and buys 8 items. Write a PYTHON program that asks for the name of the item and the price of each item, and then displays these on the screen. Refer to the print() and input() statements covered in the module. Do NOT use loops as it will be covered in later modules. Apples 2.10 Hamburger 3.25 Milk 3.49 Sugar 1.99 Bread 1.76 Deli Turkey 7.99 Pickles 3.42 Butter 2.79
A customer comes into a grocery store and buys 8 items.   Write a PYTHON program that...
A customer comes into a grocery store and buys 8 items.   Write a PYTHON program that prompts the user for the name of the item AND the price of each item, and then simply displays whatever the user typed in on the screen nicely formatted.
Use Python 3, please type. Write a Python 3 program which is an arithmetic quiz for...
Use Python 3, please type. Write a Python 3 program which is an arithmetic quiz for children. The program asks the user to calculate the multiplication of two numbers generated at random in your code. The numbers should be in the range 1 through 10. User input is shown in bold black font in the example below. You will need to use random numbers. You should type the recommended comments at the top of your code and include three test...
using python 3 2. Write a python program that finds the numbers that are divisible by...
using python 3 2. Write a python program that finds the numbers that are divisible by both 2 and 7 but not 70, or that are divisible by 57 between 1 and 1000. 3. Write a function called YesNo that receives as input each of the numbers between 1 and 1000 and returns True if the number is divisible by both 2 and 7 but not 70, or it is divisible by 57. Otherwise it returns False. 4. In your...
using python 3 2. Write a python program that finds the numbers that are divisible by...
using python 3 2. Write a python program that finds the numbers that are divisible by both 2 and 7 but not 70, or that are divisible by 57 between 1 and 1000. 3. Write a function called YesNo that receives as input each of the numbers between 1 and 1000 and returns True if the number is divisible by both 2 and 7 but not 70, or it is divisible by 57. Otherwise it returns False. 4. In your...
Write a program IN PYTHON of the JUPYTER NOOTBOOK Write a Python program that gets a...
Write a program IN PYTHON of the JUPYTER NOOTBOOK Write a Python program that gets a numeric grade (on a scale of 0-100) from the user and convert it to a letter grade based on the following table. A: 90% - 100% B 80% - 89% C 70% - 79% D 60% - 69% F <60% The program should be written so that if the user entered either a non-numeric input or a numeric input out of the 0-100 range,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT