Found 26504 Articles for Server Side Programming

Python program to find the redundancy rates for each row of a matrix

AmitDiwan
Updated on 15-Sep-2021 11:40:56

319 Views

When it is required to find the redundancy rates for every row of a matrix, a simple iteration and the ‘append’ method can be used.ExampleBelow is a demonstration of the samemy_list = [[91, 52, 12, 29, 33], [54, 54, 54, 54, 54], [11, 22, 33, 59, 95]] print("The list is :") print(my_list) my_result = [] for sub in my_list:    my_result.append(1 - len(set(sub)) / len(sub)) print("The result is :") print(my_result)OutputThe list is : [[91, 52, 12, 29, 33], [54, 54, 54, 54, 54], [11, 22, 33, 59, 95]] The result is : [0, 1, 0]ExplanationA list of ... Read More

Python - Change the signs of elements of tuples in a list

AmitDiwan
Updated on 15-Sep-2021 11:36:50

365 Views

When it is required to change the signs of the elements in a list of tuple, a simple iteration, the ‘abs’ method and the ‘append’ method can be used.ExampleBelow is a demonstration of the samemy_list = [(51, -11), (-24, -24), (11, 42), (-12, 45), (-45, 26), (-97, -4)] print("The list is :") print(my_list) my_result = [] for sub in my_list: my_result.append((abs(sub[0]), -abs(sub[1]))) print("The result is :") print(my_result)OutputThe list is : [(51, -11), (-24, -24), (11, 42), (-12, 45), (-45, 26), (-97, -4)] The result is : [(51, -11), (24, -24), (11, -42), (12, -45), (45, ... Read More

Python program to convert a list to a set based on a common element

AmitDiwan
Updated on 15-Sep-2021 11:35:21

135 Views

When it is required to convert a list to a set based on a specific common element, a method can be defined that iterates through the set using ‘enumerate’ and places a specific condition on the elements. The ‘union’ method and the ‘map’ methods are used.ExampleBelow is a demonstration of the samedef common_elem_set(my_set):    for index, val in enumerate(my_set):       for j, k in enumerate(my_set[index + 1:], index + 1): if val & k: my_set[index] = ... Read More

Python program to convert a list into a list of lists using a step value

AmitDiwan
Updated on 15-Sep-2021 11:33:00

204 Views

When it is required to convert a list into a list of lists using a step value, a method is defined that uses a simple iteration, the ‘split’ method and the ‘append’ method.ExampleBelow is a demonstration of the samedef convert_my_list(my_list): my_result = [] for el in my_list: sub = el.split(', ') my_result.append(sub) return(my_result) my_list = ['peter', 'king', 'charlie'] print("The list is :") print(my_list) print("The resultant list is :") print(convert_my_list(my_list))OutputThe list is : ['peter', 'king', 'charlie'] The ... Read More

Python Program to find the cube of each list element

AmitDiwan
Updated on 15-Sep-2021 11:30:55

1K+ Views

When it is required to find the cube of each list element, a simple iteration and the ‘append’ method are used.ExampleBelow is a demonstration of the samemy_list = [45, 31, 22, 48, 59, 99, 0] print("The list is :") print(my_list) my_result = [] for i in my_list: my_result.append(i*i*i) print("The resultant list is :") print(my_result)OutputThe list is : [45, 31, 22, 48, 59, 99, 0] The resultant list is : [91125, 29791, 10648, 110592, 205379, 970299, 0]ExplanationA list is defined and is displayed on the console.An empty list is defined.The original list is iterated over.Every element ... Read More

Print all words occurring in a sentence exactly K times

AmitDiwan
Updated on 15-Sep-2021 11:25:55

380 Views

When it is required to print all the words occurring in a sentence exactly K times, a method is defined that uses the ‘split’ method, ‘remove’ method and the ‘count’ methods. The method is called by passing the required parameters and output is displayed.ExampleBelow is a demonstration of the samedef key_freq_words(my_string, K):    my_list = list(my_string.split(" "))    for i in my_list:       if my_list.count(i) == K:          print(i)          my_list.remove(i) my_string = "hi there how are you, how are u" K = 2 print("The string is :") print(my_string) print"The repeated ... Read More

Python - Custom space size padding in Strings List

AmitDiwan
Updated on 15-Sep-2021 11:15:59

374 Views

When it is required to customize the space size padding in a list of strings, an empty list, an iteration and the ‘append’ method is used.ExampleBelow is a demonstration of the samemy_list = ["Python", "is", "great"] print("The list is :") print(my_list) lead_size = 3 trail_size = 2 my_result = [] for elem in my_list: my_result.append((lead_size * ' ') + elem + (trail_size * ' ')) print("The result is :") print(my_result)OutputThe list is : ['Python', 'is', 'great'] The result is : [' Python ', ' is ', ' great ']ExplanationA list is defined and ... Read More

Python - First occurrence of one list in another

AmitDiwan
Updated on 15-Sep-2021 11:13:46

364 Views

When it is required to find the first occurrence of one list in another list, the ‘set’ attribute and the ‘next’ method is used.ExampleBelow is a demonstration of the samemy_list_1 = [23, 64, 34, 77, 89, 9, 21] my_list_2 = [64, 10, 18, 11, 0, 21] print("The first list is :") print(my_list_1) print("The second list is :") print(my_list_2) my_list_2 = set(my_list_2) my_result = next((ele for ele in my_list_1 if ele in my_list_2), None) print("The result is :") print(my_result)OutputThe first list is : [23, 64, 34, 77, 89, 9, 21] The second list is : [64, 10, 18, ... Read More

Python - Merge Pandas DataFrame with Inner Join

AmitDiwan
Updated on 15-Sep-2021 09:55:17

1K+ Views

To merge Pandas DataFrame, use the merge() function. The inner join is implemented on both the DataFrames by setting under the “how” parameter of the merge() function i.e. −how = “inner”At first, let us import the pandas library with an alias −import pandas as pd Create DataFrame1 −dataFrame1 = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'], "Units": [100, 150, 110, 80, 110, 90] } ) Now, create DataFrame2 −dataFrame2 = pd.DataFrame( { ... Read More

Python - Calculate the variance of a column in a Pandas DataFrame

AmitDiwan
Updated on 15-Sep-2021 09:43:21

1K+ Views

To calculate the variance of column values, use the var() method. At first, import the required Pandas library −import pandas as pdCreate a DataFrame with two columns −dataFrame1 = pd.DataFrame(    { "Car": ['BMW', 'Lexus', 'Audi', 'Tesla', 'Bentley', 'Jaguar'],       "Units": [100, 150, 110, 80, 110, 90] } ) Finding Variance of "Units" column values using var() function −print"Variance of Units column from DataFrame1 = ", dataFrame1['Units'].var()In the same way, we have calculated the Variance from the 2nd DataFrame.ExampleFollowing is the complete code −import pandas as pd ... Read More

Advertisements