Grouped Summation of Tuple List in Python

AmitDiwan
Updated on 13-Mar-2021 05:32:00

177 Views

When it is required to find the grouped summation of a list of tuple, the 'Counter' method and the '+' operator need to be used.The 'Counter' is a sub-class that helps count hashable objects, i.e it creates a hash table on its own (of an iterable- like a list, tuple, and so on) when it is invoked.It returns an itertool for all of the elements with a non-zero value as the count.The '+' operator can be used to add numeric values or concatenate strings.Below is a demonstration for the same −ExampleLive Demofrom collections import Counter my_list_1 = [('Hi', 14), ... Read More

Reverse Each Tuple in a List of Tuples in Python

AmitDiwan
Updated on 13-Mar-2021 05:31:30

306 Views

When it is required to reverse each tuple in a list of tuples, the negative step slicing can be used.A list can be used to store heterogeneous values (i.e data of any data type like integer, floating point, strings, and so on). A list of tuple basically contains tuples enclosed in a list.In negative slicing, the index is accessed using negative numbers, instead of positive ones.Below is a demonstration for the same −ExampleLive Demodef reverse_tuple(my_tuple):    return [tup[::-1] for tup in my_tuple]           my_list = [(21, 22), (43, 74, 45), (76, 17, 98, 19)] print("The list ... Read More

Check If a String Is Symmetrical or Palindrome in Python

AmitDiwan
Updated on 12-Mar-2021 13:09:01

2K+ Views

When it is required to check if a string is symmetrical or it is a palindrome, a method can be defined, that uses the ‘while’ condition. Another method is defined to check the symmetry that uses the ‘while’ and ‘if’ conditions too.A palindrome is a number or string, which when read from left to right or right to left is the same value. The index values are the same.ExampleBelow is a demonstration for the same − Live Demodef check_palindrome(my_str):    mid_val = (len(my_str)-1)//2    start = 0    end = len(my_str)-1    flag = 0    while(start

Count Letter Occurrences in a String Recursively in Python

AmitDiwan
Updated on 12-Mar-2021 13:04:59

754 Views

When it is required to check the number of times a given letter occurs in a string using recursion, a method can be defined, and an ‘if’ condition can be used.The recursion computes output of small bits of the bigger problem, and combines these bits to give the solution to the bigger problem.ExampleBelow is a demonstration for the same − Live Demodef check_frequency(my_str, my_ch):    if not my_str:       return 0    elif my_str[0]==my_ch:       return 1+check_frequency(my_str[1:], my_ch)    else:       return check_frequency(my_str[1:], my_ch) my_string = input("Enter the string :") my_char = input("Enter the character ... Read More

Determine Even or Odd Number Recursively in Python

AmitDiwan
Updated on 12-Mar-2021 13:03:21

867 Views

When it is required to check if a given number is an odd number or an even number using recursion, recursion can be used.The recursion computes output of small bits of the bigger problem, and combines these bits to give the solution to the bigger problem.ExampleBelow is a demonstration for the same − Live Demodef check_odd_even(my_num):    if (my_num < 2):       return (my_num % 2 == 0)    return (check_odd_even(my_num - 2)) my_number = int(input("Enter the number that needs to be checked:")) if(check_odd_even(my_number)==True):    print("The number is even") else:    print("The number is odd!")OutputEnter the number that needs ... Read More

Display Letters Unique to Two Strings in Python

AmitDiwan
Updated on 12-Mar-2021 13:01:30

290 Views

When it is required to display the letters that occur in both strings separately, but are not repeated, the user input is taken, and the ‘list’ and ‘set’ are used to achieve the same.A list can be used to store heterogeneous values (i.e data of any data type like integer, floating point, strings, and so on). The ‘list’ method converts a given iterable to a list type.Python comes with a datatype known as ‘set’. This ‘set’ contains elements that are unique only.The set is useful in performing operations such as intersection, difference, union and symmetric difference.ExampleBelow is a demonstration for ... Read More

Display Letters in First String Not in Second String in Python

AmitDiwan
Updated on 12-Mar-2021 13:00:07

470 Views

When it is required to display the letters that are present in the first string but not in the second string, two string inputs are taken from user. The ‘set’ is used to find the difference between the two strings.Python comes with a datatype known as ‘set’. This ‘set’ contains elements that are unique only.The set is useful in performing operations such as intersection, difference, union and symmetric difference.ExampleBelow is a demonstration for the same − Live Demomy_str_1 = input("Enter the first string...") my_str_2 = input("Enter the second string...") my_result = list(set(my_str_1)-set(my_str_2)) print("The letters in first string but not in second ... Read More

Create Dictionary with First Character as Key in Python

AmitDiwan
Updated on 12-Mar-2021 12:54:12

2K+ Views

When it is required to create a dictionary with the key as the first character and the associated value as the word which is the starting of that character, the s’plit’ method, a dictionary and simple ‘if’ condition is used.ExampleBelow is a demonstration for the same − Live Demomy_string=input("Enter the string :") split_string = my_string.split() my_dict={} for elem in split_string:    if(elem[0] not in my_dict.keys()):       my_dict[elem[0]]=[]       my_dict[elem[0]].append(elem)    else:       if(elem not in my_dict[elem[0]]):          my_dict[elem[0]].append(elem) print("The dictionary created is") for k, v in my_dict.items():    print(k, ":", v)OutputEnter the ... Read More

Count Word Frequency in a String Using Dictionary in Python

AmitDiwan
Updated on 12-Mar-2021 12:51:19

7K+ Views

When it is required to count the frequency of words that appear in a string with the help of dictionary, the ‘split’ method is used to split the values, and a list comprehension is used.The list comprehension is a shorthand to iterate through the list and perform operations on it.A list can be used to store heterogeneous values (i.e data of any data type like integer, floating point, strings, and so on).ExampleBelow is a demonstration for the same − Live Demomy_string = input("Enter the string :") my_list=[] my_list=my_string.split() word_freq=[my_list.count(p) for p in my_list] print("The frequency of words is ...") print(dict(zip(my_list, word_freq)))OutputEnter ... Read More

Form Dictionary from Object of a Class in Python

AmitDiwan
Updated on 12-Mar-2021 12:49:57

3K+ Views

When it is required to form a dictionary with the help of an object and class, a class is defined. An ‘init’ function is defined, that assigns values to variables. An instance of the class is created, and the init function is called.ExampleBelow is a demonstration for the same − Live Democlass base_class(object):    def __init__(self):       self.A = 32       self.B = 60 my_instance = base_class() print("An instance of the class has been created") print(my_instance.__dict__)OutputAn instance of the class has been created {'A': 32, 'B': 60}ExplanationA ‘base_class’ is defined, and an object is passed to it.An ... Read More

Advertisements