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')...
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
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,...
Write a Python program to (a) create a new empty stack. Then, (b) add items onto...
Write a Python program to (a) create a new empty stack. Then, (b) add items onto the stack. (c) Print the stack contents. (d) Remove an item off the stack, and (e) print the removed item. At the end, (f) print the new stack contents:
1) a. Write a C++ program for the recursive algorithm that removes all occurrences of a...
1) a. Write a C++ program for the recursive algorithm that removes all occurrences of a specific character from a string b. Write the pseudocode for the program.
Python 1. A salesman can sell five different items. Write a program that lets the salesman...
Python 1. A salesman can sell five different items. Write a program that lets the salesman enter the quantity of each item sold, calculates the total sales, and prints as below. Use a for loop to ask the salesman how many of each product he sold. tem 1 $2.50 Item 2 $1.98 Item 3 $5.75 Item 4 $3.45 Item 5 $4.00 2. Rewrite program #1 using a for loop to run for 3 salesmen and print the total sales for...
JAVA Write a program that reads the integers between -100 and 100 and counts the occurrences...
JAVA Write a program that reads the integers between -100 and 100 and counts the occurrences of each with ascending order. input: line1:number of figures line2:number Sample Input 5 -3 100 -1 -2 -1 Sample Output -3 1 -2 1 -1 2 100 1
Program must be in Python Write a program in Python whose inputs are three integers, and...
Program must be in Python Write a program in Python whose inputs are three integers, and whose output is the smallest of the three values. Input is 7 15 3
Calculating Delivery Cost Program in Python write a program in Python that will ask a user...
Calculating Delivery Cost Program in Python write a program in Python that will ask a user to enter the purchase total, the number of the items that need to be delivered and delivery day. Then the system displays the cost of delivery along with the total cost. Purchase total > $150 Yes Number of the items (N) N<=5 N>=6 Delivery day Same Day Next Day Same Day Next Day Delivery charges ($) 8 N * 1.50 N * 2.50 N...
write a python program to do the following: ask a student for 3 coefficients a b...
write a python program to do the following: ask a student for 3 coefficients a b c using the high_school_quizz to display the solutions of a quadratic problem. after, ask the student if he would like another quadratic equation solved If the student answers anything but yes, the program terminates by displaying a good bye message if the student answers yes (any form of yes should be acceptable) , the student is asked for the coefficient again and the resulting...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT