Found 26504 Articles for Server Side Programming

Python – Test if tuple list has a single element

AmitDiwan
Updated on 14-Sep-2021 11:11:56

317 Views

When it is required to test if a tuple list contains a single element, a flag value and a simple iteration is used.ExampleBelow is a demonstration of the samemy_list = [(72, 72, 72), (72, 72), (72, 72)] print("The list is :") print(my_list) my_result = True for sub in my_list:    flag = True    for element in sub:       if element != my_list[0][0]:          flag = False          break    if not flag:       my_result = False       break if(flag == True): ... Read More

Python – Redistribute Trimmed Values

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

62 Views

When it is required to redistribute the trimmed values, a list comprehension and the ‘/’ operator are used.ExampleBelow is a demonstration of the samemy_list = [11, 26, 24, 75, 96, 37, 48, 29, 93] print("The list is :") print(my_list) key = 2 print("The value of key is") print(key) full_sum = sum(my_list) trimmed_list = my_list[key:len(my_list) - key] trim_sum = sum(trimmed_list) add_value = (full_sum - trim_sum) / len(trimmed_list) result = [ele + add_value for ele in trimmed_list] print("The resultant list is:") print(result)OutputThe list is : [11, 26, 24, 75, 96, 37, 48, 29, 93] The value of ... Read More

Python – Consecutive identical elements count

AmitDiwan
Updated on 14-Sep-2021 11:08:13

1K+ Views

When it is required to get the count of consecutive identical elements in a list, an iteration, the ‘append’ method, and the ‘set’ method are used.ExampleBelow is a demonstration of the samemy_list = [24, 24, 24, 15, 15, 64, 64, 71, 13, 95, 100] print("The list is :") print(my_list) my_result = [] for index in range(0, len(my_list) - 1): if my_list[index] == my_list[index + 1]: my_result.append(my_list[index]) my_result = len(list(set(my_result))) print("The result is :") print(my_result)OutputThe list is : [24, 24, 24, 15, 15, 64, 64, 71, 13, ... Read More

Python – Rows with all List elements

AmitDiwan
Updated on 14-Sep-2021 11:06:56

760 Views

When it is required to give the rows with all list elements, a flag value, a simple iteration and the ‘append’ method is used.ExampleBelow is a demonstration of the samemy_list = [[8, 6, 3, 2], [1, 6], [2, 1, 7], [8, 1, 2]] print("The list is :") print(my_list) sub_list = [1, 2] result = [] for row in my_list: flag = True for element in sub_list: if element not in row: flag = False ... Read More

Python – Filter rows without Space Strings

AmitDiwan
Updated on 14-Sep-2021 11:05:20

207 Views

When it is required to filter rows without the soace strings, a list comprehension, regular expression, the ‘not’ operator and the ‘any’ method are used.ExampleBelow is a demonstration of the sameimport re my_list = [["python is", "fun"], ["python", "good"], ["python is cool"], ["love", "python"]] print("The list is :") print(my_list) my_result = [row for row in my_list if not any(bool(re.search(r"\s", element)) for element in row)] print("The resultant list is :") print(my_result)OutputThe list is : [[‘python is’, ‘fun’], [‘python’, ‘good’], [‘python is cool’], [‘love’, ‘python’]] The resultant list is : [[‘python’, ‘good’], [‘love’, ‘python’]]ExplanationThe required packages are imported ... Read More

Python – Equidistant consecutive characters Strings

AmitDiwan
Updated on 14-Sep-2021 11:03:12

234 Views

When it is required to find equidistant consecutive character strings, a list comprehension, the ‘all’ operator and the ‘ord’ method are used.ExampleBelow is a demonstration of the samemy_list = ["abc", "egfg", "mpsv", "abed", 'xzbd', 'agms'] print("The list is :") print(my_list) my_result = [sub for sub in my_list if all(ord(sub[index + 1]) - ord(sub[index]) == ord(sub[1]) - ord(sub[0]) for index in range(0, len(sub) - 1))] print("The resultant list is :") print(my_result)OutputThe list is : ['abc', 'egfg', 'mpsv', 'abed', 'xzbd', 'agms'] The resultant list is : ['abc', 'mpsv', 'agms']ExplanationA list of string values is defined and is displayed on ... Read More

Python – Reform K digit elements

AmitDiwan
Updated on 14-Sep-2021 11:02:08

161 Views

When it is required to reform K digit elements, a list comprehension and the ‘append’ method are used.ExampleBelow is a demonstration of the samemy_list = [231, 67, 232, 1, 238, 31, 793] print("The list is :") print(my_list) K = 3 print("The value of K is ") print(K) temp = ''.join([str(ele) for ele in my_list]) my_result = [] for index in range(0, len(temp), K): my_result.append(int(temp[index: index + K])) print("The resultant list is :") print(my_result)OutputThe list is : [231, 67, 232, 1, 238, 31, 793] The value of K is 3 The resultant ... Read More

Python program to test if all y occur after x in List

AmitDiwan
Updated on 14-Sep-2021 10:55:09

266 Views

When it is required to check if all ‘y’ occurs after ‘x’ in a list, the enumerate attribute along with a specific condition is used.ExampleBelow is a demonstration of the samemy_list = [11, 25, 13, 11, 64, 25, 8, 9] print("The list is :") print(my_list) x, y = 13, 8 x_index = my_list.index(x) my_result = True for index, element in enumerate(my_list): if element == y and index < x_index: my_result = False break if(my_result == True): ... Read More

Python – Test if all rows contain any common element with other Matrix

AmitDiwan
Updated on 14-Sep-2021 10:51:16

225 Views

When it is required to test if all rows contain any common element with other matrix, a simple iteration and a flag value are used.ExampleBelow is a demonstration of the samemy_list_1 = [[3, 16, 1], [2, 4], [4, 31, 31]] my_list_2 = [[42, 16, 12], [42, 8, 12], [31, 7, 10]] print("The first list is :") print(my_list_1) print("The second list is :") print(my_list_2) my_result = True for idx in range(0, len(my_list_1)):    temp = False    for element in my_list_1[idx]:       if element in my_list_2[idx]:          temp = True   ... Read More

Python – Reorder for consecutive elements

AmitDiwan
Updated on 14-Sep-2021 10:43:04

150 Views

When it is required to reorder consecutive elements, the ‘Counter’ method, an empty list and a simple iteration are used.ExampleBelow is a demonstration of the samefrom collections import Counter my_list = [21, 83, 44, 52, 61, 72, 81, 96, 18, 44] print("The list is :") print(my_list) my_frequencys = Counter(my_list) my_result = [] for value, count in my_frequencys.items(): my_result.extend([value]*count) print("The resultant list is :") print(my_result)OutputThe list is : [21, 83, 44, 52, 61, 72, 81, 96, 18, 44] The resultant list is : [21, 83, 44, 44, 52, 61, 72, 81, 96, 18]ExplanationThe ... Read More

Advertisements