Found 26504 Articles for Server Side Programming

Python – Check Similar elements in Matrix rows

AmitDiwan
Updated on 16-Sep-2021 09:18:43

236 Views

When it is required to check for similar elements in a matrix row, a method is defined that take a matrix as parameter. The map method is used to covert the matrix to a tuple. The matrix values are iterated over and if the frequency is greater than 1, it is displayed on the console.ExampleBelow is a demonstration of the samefrom collections import Counter def find_dupes(my_matrix):    my_matrix = map(tuple, my_matrix)    freq_dict = Counter(my_matrix)    for (row, freq) in freq_dict.items():       if freq>1:          print (row) my_matrix = [[1, 1, 0, ... Read More

Python – Check alternate peak elements in List

AmitDiwan
Updated on 16-Sep-2021 09:06:52

188 Views

When it is required to check the alternate peak elements in a list, a function is defined that iterates through the list, the adjacent elements of the array are compared and depending on this, the output is displayed on the console.ExampleBelow is a demonstration of the samedef find_peak(my_array, array_length) :    if (array_length == 1) :       return 0    if (my_array[0] >= my_array[1]) :       return 0    if (my_array[array_length - 1] >= my_array[array_length - 2]) :       return array_length - 1    for i in range(1, array_length - 1) : ... Read More

Python – Extract Paired Rows

AmitDiwan
Updated on 16-Sep-2021 08:52:55

167 Views

When it is required to extract paired rows, a list comprehension and the ‘all’ operator is used.ExampleBelow is a demonstration of the samemy_list = [[10, 21, 34, 21, 37], [41, 41, 52, 68, 68, 41], [12, 29], [30, 30, 51, 51]] print("The list is :") print(my_list) my_result = [row for row in my_list if all(row.count(element) % 2 == 0 for element in row)] print("The result is :") print(my_result)OutputThe list is : [[10, 21, 34, 21, 37], [41, 41, 52, 68, 68, 41], [12, 29], [30, 30, 51, 51]] The result is : [[30, 30, 51, 51]]ExplanationA list ... Read More

Python – All replacement combination from other list

AmitDiwan
Updated on 16-Sep-2021 08:51:09

284 Views

When it is required to get the replacement combination from the other list, the ‘combinations’ method and the ‘list’ method is used.ExampleBelow is a demonstration of the samefrom itertools import combinations my_list = [54, 98, 11] print("The list is :") print(my_list) replace_list = [8, 10] my_result = list(combinations(my_list + replace_list, len(my_list))) print("The result is :") print(my_result)OutputThe list is : [54, 98, 11] The result is : [(54, 98, 11), (54, 98, 8), (54, 98, 10), (54, 11, 8), (54, 11, 10), (54, 8, 10), (98, 11, 8), (98, 11, 10), (98, 8, 10), (11, 8, ... Read More

Python - Convert one datatype to another in a Pandas DataFrame

AmitDiwan
Updated on 16-Sep-2021 08:55:23

392 Views

Use the astype() method in Pandas to convert one datatype to another. Import the required library −import pandas as pdCreate a DataFrame. Here, we have 2 columns, “Reg_Price” is a float type and “Units” int type −dataFrame = pd.DataFrame( { "Reg_Price": [7000.5057, 1500, 5000, 8000, 9000.75768, 6000], "Units": [90, 120, 100, 150, 200, 130] } ) Check the datatypes of the columns created above −dataFrame.dtypesConvert both the types to int32 −dataFrame.astype('int32').dtypes ExampleFollowing is the code −import pandas as pd # ... Read More

Python - Sort rows by Frequency of K

AmitDiwan
Updated on 16-Sep-2021 08:49:13

167 Views

When it is required to sort the rows by frequency of ‘K’, a list comprehension and ‘Counter’ methods are used.ExampleBelow is a demonstration of the samefrom collections import Counter my_list = [34, 56, 78, 99, 99, 99, 99, 99, 12, 12, 32, 51, 15, 11, 0, 0] print ("The list is ") print(my_list) my_result = [item for items, c in Counter(my_list).most_common() for item in [items] * c] print("The result is ") print(my_result)OutputThe list is [34, 56, 78, 99, 99, 99, 99, 99, 12, 12, 32, 51, 15, 11, 0, 0] The result is [99, 99, 99, ... Read More

Python - Selective consecutive Suffix Join

AmitDiwan
Updated on 16-Sep-2021 08:47:24

149 Views

When it is required to find the selective consecutive suffix join, a simple iteration, the ‘endswith’ method and the ‘append’ method can be used.ExampleBelow is a demonstration of the samemy_list = ["Python-", "fun", "to-", "code"] print("The list is :") print(my_list) suffix = '-' print("The suffix is :") print(suffix) result = [] temp = [] for element in my_list: temp.append(element) if not element.endswith(suffix): result.append(''.join(temp)) temp = [] print("The result is :") print(result)OutputThe list is : ['Python-', 'fun', 'to-', ... Read More

Python – Center align column headers of a Pandas DataFrame

AmitDiwan
Updated on 16-Sep-2021 08:48:12

6K+ Views

To center align column headers, use the display.colheader_justify and ‘center’ value. Import the require library −import pandas as pdCreate a DataFrame with 2 columns −dataFrame = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'], "Reg_Price": [7000.5057, 1500, 5000.9578, 8000, 9000.75768, 6000] } )Now, center align the column headers −pd.set_option('display.colheader_justify', 'center') ExampleFollowing is the code −import pandas as pd # Create DataFrame dataFrame = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', ... Read More

Python Program that print elements common at specified index of list elements

AmitDiwan
Updated on 16-Sep-2021 08:44:53

480 Views

When it is required to print the elements common at a specific index in a list of strings, a ‘min’ method, list comprehension and a Boolean flag value can be used.ExampleBelow is a demonstration of the samemy_list = ["week", "seek", "beek", "reek", 'meek', 'peek'] print("The list is :") print(my_list) min_length = min(len(element) for element in my_list) my_result = [] for index in range(0, min_length): flag = True for element in my_list: if element[index] != my_list[0][index]: ... Read More

Python Program to print element with maximum vowels from a List

AmitDiwan
Updated on 16-Sep-2021 08:42:08

636 Views

When it is required to print element with maximum vowels from a list, a list comprehension is used.ExampleBelow is a demonstration of the samemy_list = ["this", "week", "is", "going", "great"] print("The list is :") print(my_list) my_result = "" max_length = 0 for element in my_list: vowel_length = len([element for element in element if element in ['a', 'e', 'o', 'u', 'i']]) if vowel_length > max_length: max_length = vowel_length my_result = element print("The result is :") print(my_result)OutputThe list is ... Read More

Advertisements