Day: March 7, 2021

Python Example – Write a Python program to perform Counter arithmetic and set operations for aggregating results

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to perform Counter arithmetic and set operations for aggregating results.   Sample Solution: Python Code: import collections c1 = collections.Counter([1, 2, 3, 4, 5]) c2 = collections.Counter([4, 5, 6, 7, 8]) print(‘C1:’, c1) print(‘C2:’, c2) print(‘nCombined counts:’) print(c1 + c2) print(‘nSubtraction:’) …

Python Example – Write a Python program to find the most common element of a given list

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to find the most common element of a given list.   Sample Solution: Python Code: from collections import Counter language = [‘PHP’, ‘PHP’, ‘Python’, ‘PHP’, ‘Python’, ‘JS’, ‘Python’, ‘Python’,’PHP’, ‘Python’] print(“Original list:”) print(language) cnt = Counter(language) print(“nMost common element of the …

Python Example – Write a Python program to count the number of times a specific element presents in a deque object.

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to count the number of times a specific element presents in a deque object.   Sample Solution: Python Code: import collections nums = (2,9,0,8,2,4,0,9,2,4,8,2,0,4,2,3,4,0) nums_dq = collections.deque(nums) print(“Number of 2 in the sequence”) print(nums_dq.count(2)) print(“Number of 4 in the sequence”) print(nums_dq.count(4)) …

Python Example – Write a Python program to copy of a deque object and verify the shallow copying process

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to copy of a deque object and verify the shallow copying process.   Sample Solution: Python Code: import collections tup1 = (1,3,5,7,9) dq1 = collections.deque(tup1) dq2 = dq1.copy() print(“Content of dq1:”) print(dq1) print(“dq2 id:”) print(id(dq1)) print(“nContent of dq2:”) print(dq2) print(“dq2 id:”) …

Python Example – Write a Python program to remove all the elements of a given deque object.

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to remove all the elements of a given deque object.   Sample Solution: Python Code: import collections odd_nums = (1,3,5,7,9) odd_deque = collections.deque(odd_nums) print(“Original Deque object with odd numbers:”) print(odd_deque) print(“Deque length: %d”%(len(odd_deque))) odd_deque.clear() print(“Deque object after removing all numbers-“) print(odd_deque) …

Python Example – Write a Python program to add more number of elements to a deque object from an iterable object.

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to add more number of elements to a deque object from an iterable object.   Sample Solution: Python Code: import collections even_nums = (2, 4, 6, 8, 10) even_deque = collections.deque(even_nums) print(“Even numbers:”) print(even_deque) more_even_nums = (12, 14, 16, 18, 20) …

Python Example – Write a Python program to create a deque from an existing iterable object.

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to create a deque from an existing iterable object.   Sample Solution: Python Code: import collections even_nums = (2, 4, 6) print(“Original tuple:”) print(even_nums) print(type(even_nums)) even_nums_deque = collections.deque(even_nums) print(“\nOriginal deque:”) print(even_nums_deque) even_nums_deque.append(8) even_nums_deque.append(10) even_nums_deque.append(12) even_nums_deque.appendleft(2) print(“New deque from an existing iterable …

Python Example – Write a Python program to create a deque and append few elements to the left and right, then remove some elements from the left, right sides and reverse the deque

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to create a deque and append few elements to the left and right, then remove some elements from the left, right sides and reverse the deque.   Sample Solution: Python Code: import collections deque_colors = collections.deque([“Red”,”Green”,”White”]) print(deque_colors) print(“\nAdding to the left: …

Python Example – Write a Python program to find the occurrences of 10 most common words in a given text.

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to find the occurrences of 10 most common words in a given text.   Sample Solution: Python Code: from collections import Counter import re text = “””The Python Software Foundation (PSF) is a 501(c)(3) non-profit corporation that holds the intellectual property …

Python Example – Write a Python program to find the most common elements and their counts of a specified text.

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to find the most common elements and their counts of a specified text.   Sample Solution: Python Code: from collections import Counter s = ‘lkseropewdssafsdfafkpwe’ print(“Original string: “+s) print(“Most common three characters of the said string:”) print(Counter(s).most_common(3)) Sample Output: Original string: …