Found 10476 Articles for Python

Python - Convert List of lists to List of Sets

AmitDiwan
Updated on 21-Sep-2021 08:29:02

836 Views

When it is required to convert a list of list to a list of set, the ‘map’, ‘set’, and ‘list’ methods are used.ExampleBelow is a demonstration of the samemy_list = [[2, 2, 2, 2], [1, 2, 1], [1, 2, 3], [1, 1], [0]] print("The list of lists is: ") print(my_list) my_result = list(map(set, my_list)) print("The resultant list is: ") print(my_result)OutputThe list of lists is: [[2, 2, 2, 2], [1, 2, 1], [1, 2, 3], [1, 1], [0]] The resultant list is: [set([2]), set([1, 2]), set([1, 2, 3]), set([1]), set([0])]ExplanationA list of list is defined and is displayed ... Read More

Python program to get all subsets having sum s

AmitDiwan
Updated on 21-Sep-2021 08:27:19

712 Views

When it is required to get all the subset having a specific sum ‘s’, a method is defined that iterates through the list and gets all combinations of the list, and if it matches the sum, it is printed on the console.ExampleBelow is a demonstration of the samefrom itertools import combinations def sub_set_sum(size, my_array, sub_set_sum):    for i in range(size+1):       for my_sub_set in combinations(my_array, i):          if sum(my_sub_set) == sub_set_sum:           print(list(my_sub_set)) my_size = 6 my_list = [21, 32, 56, 78, 45, 99, 0] ... Read More

Python - Filter Rows Based on Column Values with query function in Pandas?

AmitDiwan
Updated on 21-Sep-2021 08:30:34

681 Views

To filter rows based on column values, we can use the query() function. In the function, set the condition through which you want to filter records. At first, import the required library −import pandas as pdFollowing is our data with Team Records −Team = [['India', 1, 100], ['Australia', 2, 85], ['England', 3, 75], ['New Zealand', 4 , 65], ['South Africa', 5, 50], ['Bangladesh', 6, 40]]Create a DataFrame from above and add columns as well −dataFrame = pd.DataFrame(Team, columns=['Country', 'Rank', 'Points']) Use query() to filter records with “Rank” equal to 5 −dataFrame.query("Rank == 5"))ExampleFollowing is the complete code −import pandas as ... Read More

Python - Find all the strings that are substrings to the given list of strings

AmitDiwan
Updated on 21-Sep-2021 08:24:09

581 Views

When it is required to find all the strings that are substrings of a given list of strings, the ‘set’ and ‘list’ attributes are used.ExampleBelow is a demonstration of the samemy_list_1 = ["Hi", "there", "how", "are", "you"] my_list_2 = ["Hi", "there", "how", "have", "you", 'been'] print("The first list is :") print(my_list_1) print("The second list is :") print(my_list_2) my_result = list(set([elem_1 for subset_1 in my_list_1 for elem_1 in my_list_2 if elem_1 in subset_1])) print("The result is :") print(my_result)OutputThe first list is : ['Hi', 'there', 'how', 'are', 'you'] The second list is : ['Hi', 'there', 'how', 'have', 'you', 'been'] The ... Read More

Python program to print sorted number formed by merging all elements in array

AmitDiwan
Updated on 21-Sep-2021 08:22:06

220 Views

When it is required to print the sorted numbers that are formed by merging the elements of an array, a method can be defined that first sorts the number and converts the number to an integer. Another method maps this list to a string, and is sorted again.ExampleBelow is a demonstration of the samedef get_sorted_nums(my_num): my_num = ''.join(sorted(my_num)) my_num = int(my_num) print(my_num) def merged_list(my_list): my_list = list(map(str, my_list)) my_str = ''.join(my_list) get_sorted_nums(my_str) my_list = [7, 845, 69, 60, ... Read More

Python Pandas – Can we use & Operator to find common columns between two DataFrames?

AmitDiwan
Updated on 21-Sep-2021 08:23:20

267 Views

Yes, we can use the & operator to find the common columns between two DataFrames. At first, let us create two DataFrames −# creating dataframe1 dataFrame1 = pd.DataFrame({"Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'], "Cubic_Capacity": [2000, 1800, 1500, 2500, 2200, 3000], }) print("Dataframe1...", dataFrame1) # creating dataframe2 dataFrame2 = pd.DataFrame({"Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'], "Units_Sold": [ 100, 110, 150, 80, 200, 90] })Get the common columns using the & operator −res = dataFrame1.columns & dataFrame2.columns ExampleFollowing is the code −import pandas as pd # creating dataframe1 dataFrame1 = pd.DataFrame({"Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'], ... Read More

Python Program to get all unique keys from a List of Dictionaries

AmitDiwan
Updated on 21-Sep-2021 08:19:49

2K+ Views

When it is required to get all the unique keys from a list of dictionary, the dictionary values are iterated over and converted into a set. This is converted to a list and displayed on the console.ExampleBelow is a demonstration of the samemy_list = [{'hi' : 11, 'there' : 28}, {'how' : 11, 'are' : 31}, {'you' : 28, 'Will':31}] print("The list is:") print(my_list) my_result = list(set(value for dic in my_list for value in dic.values())) print("The result is :") print(my_result)OutputThe list is: [{'there': 28, 'hi': 11}, {'how': 11, 'are': 31}, {'Will': 31, 'you': 28}] The result is : ... Read More

Python Program to print all distinct uncommon digits present in two given numbers

AmitDiwan
Updated on 21-Sep-2021 08:18:23

217 Views

When it is required to print all the distinct uncommon digits that are present in two numbers, a method is defined that takes two integers as parameters. The method ‘symmetric_difference’ is used to get the uncommon digits.ExampleBelow is a demonstration of the samedef distinct_uncommon_nums(val_1, val_2): val_1 = str(val_1) val_2 = str(val_2) list_1 = list(map(int, val_1)) list_2 = list(map(int, val_2)) list_1 = set(list_1) list_2 = set(list_2) my_list = list_1.symmetric_difference(list_2) my_list = list(my_list) my_list.sort(reverse ... Read More

Python Program to get K length groups with given summation

AmitDiwan
Updated on 21-Sep-2021 08:16:34

203 Views

When it is required to get ‘K’ length groups with a given summation, an empty list, the ‘product’ method, the ‘sum’ method and the ‘append’ method can be used.ExampleBelow is a demonstration of the samefrom itertools import product my_list = [45, 32, 67, 11, 88, 90, 87, 33, 45, 32] print("The list is : ") print(my_list) N = 77 print("The value of N is ") print(N) K = 2 print("The value of K is ") print(K) my_result = [] for sub in product(my_list, repeat = K): if sum(sub) == N: ... Read More

Python Program to Split joined consecutive similar characters

AmitDiwan
Updated on 21-Sep-2021 08:14:57

289 Views

When it is required to split the joined consecutive characters that are similar in nature, the ‘groupby’ method and the ‘join’ method are used.ExampleBelow is a demonstration of the samefrom itertools import groupby my_string = 'pppyyytthhhhhhhoooooonnn' print("The string is :") print(my_string) my_result = ["".join(grp) for elem, grp in groupby(my_string)] print("The result is :") print(my_result)OutputThe original string is : pppyyytthhhhhhhooonnn The resultant split string is : ['ppp', 'yyy', 'tt', 'hhhhhhh', 'ooo', 'nnn']ExplanationThe required packages are imported into the environment.A string is defined and it is displayed on the console.The string is iterated over and it is sorted using ... Read More

Advertisements