Find Distance Between First and Last Even Elements in a List

AmitDiwan
Updated on 20-Sep-2021 06:48:21

419 Views

When it is required to find the distance between the first and last even elements of a list, list elements are accessed using indexing and the difference is found.ExampleBelow is a demonstration of the samemy_list = [2, 3, 6, 4, 6, 2, 9, 1, 14, 11] print("The list is :") print(my_list) my_indices_list = [idx for idx in range( len(my_list)) if my_list[idx] % 2 == 0] my_result = my_indices_list[-1] - my_indices_list[0] print("The result is :") print(my_result)OutputThe list is : [2, 3, 6, 4, 6, 2, 9, 1, 14, 11] The result is : 8ExplanationA list ... Read More

Filter Rows with Only Alphabets from List of Lists in Python

AmitDiwan
Updated on 20-Sep-2021 06:45:59

491 Views

When it is required to filter the rows that contains only alphabets in a list of lists, the list is iterated over and the ‘isalpha’ method is used to check if an alphabet is present or not.ExampleBelow is a demonstration of the samemy_list = [["python", "is", "best"], ["abc123", "good"], ["abc def ghij"], ["abc2", "gpqr"]] print("The list is :") print(my_list) my_result = [sub for sub in my_list if all(element.isalpha() for element in sub)] print("The result is :") print(my_result)OutputThe list is : [['python', 'is', 'best'], ['abc123', 'good'], ['abc def ghij'], ['abc2', 'gpqr']] The result is : [['python', 'is', 'best']]ExplanationA list ... Read More

Find the Sum of Length of Strings at Given Indices in Python

AmitDiwan
Updated on 20-Sep-2021 06:43:55

299 Views

When it is required to find the sum of the length of string at specific indices, the ‘enumerate’ is used to iterate through the elements in the list and adding the length of the element to a list.ExampleBelow is a demonstration of the samemy_list = ["python", "is", "best", "for", "coders"] print("The list is :") print(my_list) index_list = [0, 1, 4] result = 0 for index, element in enumerate(my_list): if index in index_list: result += len(element) print("The result is :") print(result)OutputThe list is : ['python', 'is', 'best', 'for', ... Read More

Test If Elements of List Are in Min-Max Range from Another List in Python

AmitDiwan
Updated on 20-Sep-2021 06:42:12

576 Views

When it is required to test if the elements are in the min/max range, the list elements are iterated over, and are checked to see if it is equal to ‘max’ value.ExampleBelow is a demonstration of the samemy_list = [5, 6, 4, 7, 8, 13, 15] print("The list is : ") print(my_list) range_list = [4, 7, 10, 6] my_result = True for elem in range_list: if elem!= max(my_list): my_result = False break if(elem == True): print("All the elements are ... Read More

Mean Deviation of Elements in Python

AmitDiwan
Updated on 20-Sep-2021 06:37:24

812 Views

When it is required to find the mean deviation of the elements of a list, the ‘sum’ method and the ‘len’ method is used.ExampleBelow is a demonstration of the samemy_list = [3, 5, 7, 10, 12] print("The list is :") print(my_list) my_mean = sum(my_list) / len(my_list) my_variance = sum([((x – my_mean) ** 2) for x in my_list]) / len(my_list) my_result = my_variance ** 0.5 print("The result is :") print(result)OutputThe original list : [3, 5, 7, 10, 12] the standard deviation of list is : 3.2619012860600183ExplanationA list is defined and is displayed on the console.The ‘sum’ of the ... Read More

Set Labels in Matplotlib Hlines

Rishikesh Kumar Rishi
Updated on 19-Sep-2021 08:10:07

4K+ Views

To set labels in matplotlib.hlines, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Add a horizontal line across the axis, y=1, with y=1 label, color='orange'.Add a horizontal line across the axis, y=2, with y=2 label, color='red'.To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt # Set the figure size plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True # Add horizontal line plt.hlines(y=1, xmin=1, xmax=4, lw=7, color='orange') plt.text(4, 1, 'y=1', ha='left', va='center') # Add another horizontal line plt.hlines(y=2, xmin=2, xmax=5, lw=7, color='red') plt.text(2, 2, 'y=2', ha='right', va='center') ... Read More

Make a Scatter Plot for Clustering in Python

Rishikesh Kumar Rishi
Updated on 19-Sep-2021 08:00:50

7K+ Views

To make a scatter plot for clustering in Python, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create x and y data points, Cluster and centers using numpy.Create a new figure or activate an existing figure.Add a subplot arrangement to the current figure.Plot the scatter data points using scatter() method.Iterate centers data and place marker using scatter() method.To display the figure, use show() method.Exampleimport numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = np.random.randn(10) y = np.random.randn(10) Cluster = np.array([0, 1, ... Read More

Put a Circle with Annotation in Matplotlib

Rishikesh Kumar Rishi
Updated on 19-Sep-2021 07:28:03

3K+ Views

To put a circle with annotation in matplotlib, we can take the following steps −Set the figure size and adjust the padding between and around the subplots.Create data points using numpy.Get the point coordinate to put circle with annotation.Get the current axis.Plot the data and data points using plot() method.Set X and Y axes scale.To put a circled marker, use the plot() method with marker='o' and some properties.Annotate that circle (Step 7) with arrow style.To display the figure, use show() method.Exampleimport matplotlib.pyplot as plt import numpy as np plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True data = np.array([[5, ... Read More

Make a 3D Scatter Plot in Python

Rishikesh Kumar Rishi
Updated on 19-Sep-2021 07:08:45

2K+ Views

To get a 3D plot, we can use fig.add_subplot(111, projection='3d') method to instantiate the axis. After that, we can use the scatter method to draw different data points on the x, y, and z axes.StepsCreate a new figure, or activate an existing figure.Add an `~.axes.Axes` to the figure as part of a subplot arrangement, where nrows = 1, ncols = 1, index = 1 and projection is ‘3d’.Iterate a list of marks, xs, ys and zs, to make scatter points.Set x, y, and z labels using set_xlabel, y_label, and z_label methods.Use plt.show() method to plot the figure.Exampleimport matplotlib.pyplot as plt import ... Read More

Difference Between Single Phase and Three Phase Induction Motor

Manish Kumar Saini
Updated on 18-Sep-2021 07:43:16

26K+ Views

The differences between a single-phase and a three-phase induction motor are given below −A 1-phase induction motor requires single-phase AC supply, whereas a 3-phase induction motor need a source of 3-phase AC supply for its operation.1-phase induction motors produce low starting torque, whereas 3phase induction motors produce high starting torque.The efficiency of a 1-phase induction motor is less as compared to a 3phase induction motor.The power factor of a 1-phase induction motor is low, while the power factor of a 3-phase induction motor is high.1-phase induction motors are simple in construction, reliable and economical as compared to 3phase induction motors.1-phase ... Read More

Advertisements