Programming Articles - Page 1093 of 3363

Python Program that extract words starting with Vowel From A list

AmitDiwan
Updated on 07-Sep-2021 19:52:11

2K+ Views

When it is required to extract words starting with vowel from a list, a simple iteration, a flag value and the ‘startswith’ method are used.Below is a demonstration of the same −Example:my_list = ["abc", "phy", "and", "okay", "educate", "learn", "code"] print("The list is :") print(my_list) my_result = [] my_vowel = "aeiou" print("The vowels are ") print(my_vowel) for index in my_list: my_flag = False for element in my_vowel: if index.startswith(element): my_flag = True ... Read More

Python program to print Rows where all its Elements’ frequency is greater than K

AmitDiwan
Updated on 07-Sep-2021 12:56:19

200 Views

When it is required to print rows where all its elements’ frequency is greater than K, a method is defined that takes two parameters, and uses ‘all’ operator and iteration to give the result.Below is a demonstration of the same −Exampledef frequency_greater_K(row, K) : return all(row.count(element) > K for element in row) my_list = [[11, 11, 32, 43, 12, 23], [42, 14, 55, 62, 16], [11, 11, 11, 11], [42, 54, 61, 18]] print("The tuple is :") print(my_list) K = 1 print("The value of K is :") print(K) my_result = [row for row in my_list if frequency_greater_K(row, ... Read More

Python Program to repeat elements at custom indices

AmitDiwan
Updated on 07-Sep-2021 12:22:36

124 Views

When it is required to repeat elements at custom indices, a simple iteration, enumerate attribute, the ‘extend’ method and the ‘append’ method are used.Below is a demonstration of the same −Examplemy_list = [34, 56, 77, 23, 31, 29, 62, 99] print("The list is :") print(my_list) index_list = [3, 1, 4, 6] my_result = [] for index, element in enumerate(my_list): if index in index_list: my_result.extend([element, element]) else : my_result.append(element) print("The result is :") print(my_result)OutputThe list is : [34, 56, 77, 23, 31, 29, ... Read More

Python Program to Find the Fibonacci Series Using Recursion

AmitDiwan
Updated on 07-Sep-2021 10:59:44

2K+ Views

When it is required to find the Fibonacci sequence using the method of recursion, a method named ‘fibonacci_recursion’ is defined, that takes a value as parameter. It is called again and again by reducing the size of the input.Below is a demonstration of the same:Exampledef fibonacci_recursion(my_val): if my_val

Python – Inverse Dictionary Values List

AmitDiwan
Updated on 07-Sep-2021 10:40:32

389 Views

When it is required to inverse the dictionary values to a list, a simple iteration and ‘append’ method is used.Below is a demonstration of the same −from collections import defaultdict my_dict = {13: [12, 23], 22: [31], 34: [21], 44: [52, 31]} print("The dictionary is :") print(my_dict) my_result = defaultdict(list) for keys, values in my_dict.items(): for val in values: my_result[val].append(keys) print("The result is :") print(dict(my_result))OutputThe dictionary is : {34: [21], 44: [52, 31], 13: [12, 23], 22: [31]} The result is : {52: [44], 31: [44, 22], 12: [13], 21: [34], ... Read More

Python – Sort String list by K character frequency

AmitDiwan
Updated on 07-Sep-2021 09:43:22

395 Views

When it is required to sort a list of strings based on the ‘K’ number of character frequency, the ‘sorted’ method, and the lambda function is used.ExampleBelow is a demonstration of the same −my_list = ['Hi', 'Will', 'Jack', 'Python', 'Bill', 'Mills', 'goodwill'] print("The list is : " ) print(my_list) my_list.sort() print("The list after sorting is ") print(my_list) K = 'l' print("The value of K is ") print(K) my_result = sorted(my_list, key = lambda ele: -ele.count(K)) print("The resultant list is : ") print(my_result)OutputThe list is : ['Hi', 'Will', 'Jack', 'Python', 'Bill', 'Mills', 'goodwill'] The list after sorting is ... Read More

Pandas GroupBy – Count the occurrences of each combination

AmitDiwan
Updated on 07-Sep-2021 09:14:11

2K+ Views

To groupby columns and count the occurrences of each combination in Pandas, we use the DataFrame.groupby() with size(). The groupby() method separates the DataFrame into groups.At first, let us import the pandas library with an alias pd −import pandas as pdInitialize the data of lists −# initializing the data mylist = {'Car': ['BMW', 'Mercedes', 'Lamborgini', 'Audi', 'Mercedes', 'Porche', 'RollsRoyce', 'BMW'], 'Place': ['Delhi', 'Hyderabad', 'Chandigarh', 'Bangalore', 'Hyderabad', 'Mumbai', 'Mumbai', 'Delhi'], 'Sold': [95, 80, 80, 75, 90, 90, 95, 50 ]}Next, we will create a DataFrame −# DataFrame dataFrame = pd.DataFrame(mylist, columns=['Car', 'Place', 'Sold'])Now, use the groupby() to count the occurrence with ... Read More

Python Program to extracts elements from a list with digits in increasing order

AmitDiwan
Updated on 06-Sep-2021 09:15:38

182 Views

When it is required to extracts elements from a list with digits in increasing order, a simple iteration, a flag value and the ‘str’ method is used.Below is a demonstration of the same −Example Live Demomy_list = [4578, 7327, 113, 3467, 1858] print("The list is :") print(my_list) my_result = [] for element in my_list:    my_flag = True    for index in range(len(str(element)) - 1):       if str(element)[index + 1]

Python – Dual Tuple Alternate summation

AmitDiwan
Updated on 06-Sep-2021 09:13:39

202 Views

When it is required to perform dual tuple alternate summation, a simple iteration and the modulus operator are used.Below is a demonstration of the same −Example Live Demomy_list = [(24, 11), (45, 66), (53, 52), (77, 51), (31, 10)] print("The list is :") print(my_list) my_result = 0 for index in range(len(my_list)):    if index % 2 == 0:       my_result += my_list[index][0]    else:       my_result += my_list[index][1] print("The result is :") print(my_result)OutputThe list is : [(24, 11), (45, 66), (53, 52), (77, 51), (31, 10)] The result is : 225ExplanationA list of ... Read More

Python – Extract Rear K digits from Numbers

AmitDiwan
Updated on 06-Sep-2021 08:56:24

241 Views

When it is required to extract rear K digits from numbers, a simple list comprehension, the modulo operator and the ‘**’ operator are used.Below is a demonstration of the same −Example Live Demomy_list = [51645, 24567, 36743, 89452, 2122] print("The list is :") print(my_list) K = 3 print("The value of K is ") print(K) my_result = [element % (10 ** K) for element in my_list] print("The result is :") print(my_result)OutputThe list is : [51645, 24567, 36743, 89452, 2122] The value of K is 3 The result is : [645, 567, 743, 452, 122]ExplanationA list is defined and ... Read More

Advertisements