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

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.
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...
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
Define the Poisson distribution when you are interested in the number of times an event occurs...
Define the Poisson distribution when you are interested in the number of times an event occurs in a given area of opportunity. Elaborate how the Area of opportunity is a continuous unit or interval of time, volume, or such area in which more than one occurrence of an event can occur?
Define the Poisson distribution when you are interested in the number of times an event occurs...
Define the Poisson distribution when you are interested in the number of times an event occurs in a given area of opportunity. Elaborate how the Area of opportunity is a continuous unit or interval of time, volume, or such area in which more than one occurrence of an event can occur?
Code using R or Python We observe from our campus the temperature and count the number...
Code using R or Python We observe from our campus the temperature and count the number of squirrels. Our observations are T = [52, 52, 50, 54, 50, 52, 54, 80, 80] Sq = [8, 10, 6, 9, 6, 12, 12, 1, 0] a) What is the covariance of these vectors? (1point) b) What is the covariance matrix? (1point) c) What is the correlation coefficient? (1point) d) Find the coefficient of determination? (1point) e) Can you make any statement or...
C LANGUAGE ONLY Write a C program to count the frequency of each element in an...
C LANGUAGE ONLY Write a C program to count the frequency of each element in an array. Enter the number of elements to be stored in the array: 3 Input 3 elements of the array: element [0]: 25 element [1]: 12 element [2]: 43 Expected output: The frequency of all elements of an array: 25 occurs 1 times 12 occurs 1 times 3 occurs 1 times
The element titanium occurs on earth as titanium (IV) oxide and is found mostly in the...
The element titanium occurs on earth as titanium (IV) oxide and is found mostly in the mineral called rutile. Rutile is abundant in beach sands in Australia and South Africa. For titanium to be useful, rutile has to be processed to obtain the basic metal form of titanium. Although the process is costly, titanium is gaining popularity as a material for various equipment due to its strength and light weight. One such application is in sports equipment. 1) Explain the...
An element has an atomic number of 76. The number of protons and electrons in a...
An element has an atomic number of 76. The number of protons and electrons in a neutral atom of the element are?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT