Question

In: Computer Science

Python...Count the number of times a element of list1 occurs in in list2 list1 = ['a',...

Python...Count the number of times a element of list1 occurs in in list2

list1 = ['a', 'b', 'c','e','j'];

list2 = ['a', 'c', 'd', 'b','e','z'];

{'a': 1, 'c': 1, 'b': 1, 'e': 1, 'j': 0}

How do I get to this to work without the Collections counter?

Solutions

Expert Solution

Code Explanation:
1. Define two list list1 and list2
2. Create an empty dictionary res = {}
3. run a for loop i = 0 to length of list1
4. initialize count = 0
5. run a for loop from j = 0 to length of list2
6. Check whether the element of list1 is equal to list2. If yes increment count
7. After every element in list2 is checked inner loop exists. Then add that key and 
   count to dictionary res
8. The loop will check similarly for all elements in list1. and adds the keys and count to res
9. print the res.

Code:

list1 = ['a', 'b','c','e','j']
list2 = ['a','c','d','b','e','z']
res ={}
for i in range(len(list1)):
    count = 0
    for j in range(len(list2)):
        if(list1[i] == list2[j]):
            count = count+1
    res[list1[i]] = count
print(res)

O/P:

{'a': 1, 'b': 1, 'c': 1, 'e': 1, 'j': 0}
Execution screenshot for the above code:

Sample O/P when

list1 = ['a', 'b','c','e','j']
list2 = ['a','c','a','d','e','b','e','z']

(If you still have any doubts regarding this answer please comment I will definitely help)


Related Solutions

By using Python code: 1. list1 = ['smith', 'john', 'andy'] list2 = ['kim', 'mary', 'paul'] Write...
By using Python code: 1. list1 = ['smith', 'john', 'andy'] list2 = ['kim', 'mary', 'paul'] Write a python program which does the following: a. add items in list2 to list1 b. Remove mary from list1 c. Insert sam at 5th position d. Sort the elements of list1 in ascending order 2. Write a python program that asks the user to enter a sentence and creates a list of words in that sentence. 3. friends = ('sam','andy','mary','andy','john','andy','mary') Write a python program...
Make a python dictionary that counts the number of times each word occurs in the the...
Make a python dictionary that counts the number of times each word occurs in the the book below book_url = 'http://www.gutenberg.org/cache/epub/2680/pg2680.txt'
Make a python dictionary that counts the number of times each word occurs in the the...
Make a python dictionary that counts the number of times each word occurs in the the book below book_url = 'http://www.gutenberg.org/cache/epub/2680/pg2680.txt' without downloading to computer.
//C++ a. Add the following operation to the class orderedLinkedList: void mergeLists(orderedLinkedList &list1, orderedLinkedList &list2); //This...
//C++ a. Add the following operation to the class orderedLinkedList: void mergeLists(orderedLinkedList &list1, orderedLinkedList &list2); //This function creates a new list by merging the //elements of list1 and list2. //Postcondition: first points to the merged list // list1 and list2 are empty Consider the following statements: orderedLinkedList newList; orderedLinkedList list1; orderedLinkedList list2; Suppose list1 points to the list with the elements 2 6 7, and list2 points to the list with the elements 3 5 8. The statement: newList.mergeLists(list1, list2);...
This program is about list comprehension. list1 = [2, 5, 7, 8] list2 = [1, 2]...
This program is about list comprehension. list1 = [2, 5, 7, 8] list2 = [1, 2] (e) Use nested list comprehension with list1 and list2 as input sequences to generate this list: [[3, 4], [6, 7], [8, 9], [9, 10]] Display the list. The following is the expected output. Part e: [[3, 4], [6, 7], [8, 9], [9, 10]]
In C++ For this assignment, you will write a program to count the number of times...
In C++ For this assignment, you will write a program to count the number of times the words in an input text file occur. The WordCount Structure Define a C++ struct called WordCount that contains the following data members: An array of 31 characters named word An integer named count Functions Write the following functions: int main(int argc, char* argv[]) This function should declare an array of 200 WordCount objects and an integer numWords to track the number of array...
Method: ArrayList<Integer> diff(ArrayList<Integer> list1, ArrayList<Integer> list2) diff() method accepts two ArrayLists of Integer and returns the...
Method: ArrayList<Integer> diff(ArrayList<Integer> list1, ArrayList<Integer> list2) diff() method accepts two ArrayLists of Integer and returns the union of elements in two lists. For example: list1 contains elements [1, 2, 3, 4, 5] list2 contains elements [3, 4, 5, 6, 7] Diff() method should return an array list with elements [1, 2, 3, 4, 5, 6, 7].
Python. Write a code that asks the user to enter a string. Count the number of...
Python. Write a code that asks the user to enter a string. Count the number of different vowels ( a, e, i, o, u) that are in the string and print out the total. You may need to write 5 different if statements, one for each vowel. Enter a string: mouse mouse has 3 different vowels
PYTHON PROBLEM Given two numbers a and b, count how many times each of the digits...
PYTHON PROBLEM Given two numbers a and b, count how many times each of the digits 0 to 9 occur in all numbers between a and b, inclusive. In order to make your code simpler, implement first the function intToList(n) that takes as input one integer n, and returns a list with the digits of n in the order they appear. For example, intToList(1964) should return [1,9,6,4]. Using the function intToList, implement the function digitsCount(a, b) that returns a list...
Posting together because they are related. Python 1. Create a program to count the number of...
Posting together because they are related. Python 1. Create a program to count the number of occurrences of each letter of the alphabet in a text file, and print out the total for each letter (if greater than 0). Ignore numbers, punctuation, and special characters. Make sure to treat capital and lowercase letters as occurrences of the same letter. The text file (sample_text.txt) is attached to the assignment. Example: For the text "My dog's name is Winston." The results would...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT