In Python, finding the highest and lowest values in a list is a common task. However, if you need to locate successive pairs of the highest and lowest values along with their respective index values, a more tailored approach is required. This article will guide you through the process of implementing a Python function to achieve this specific task using the given list as an example.
1. Implementation.
- Source code.
def find_successive_extremes(input_list): # Initialize variables to store results result_pairs = [] # Initialize variables to track current highest and lowest values and their indices current_highest = float('-inf') current_lowest = float('inf') current_highest_index = None current_lowest_index = None counter = 0 # Iterate through the input list for i, num in enumerate(input_list): counter += 1 # Check for a new highest value if num > current_highest: current_highest = num current_highest_index = i # if the num is the last value in the list if counter == len(input_list): # Append the pair to the result result_pairs.append({ 'highest_value': current_highest, 'highest_index': current_highest_index, 'lowest_value': current_lowest, 'lowest_index': current_lowest_index }) # Check for a new lowest value after the highest value elif num < current_lowest: current_lowest = num current_lowest_index = i # if the num is the last value in the list if counter == len(input_list): # Append the pair to the result result_pairs.append({ 'highest_value': current_highest, 'highest_index': current_highest_index, 'lowest_value': current_lowest, 'lowest_index': current_lowest_index }) elif num > current_lowest: # Check for a new highest value after the lowest value if current_lowest_index is not None: # Append the pair to the result result_pairs.append({ 'highest_value': current_highest, 'highest_index': current_highest_index, 'lowest_value': current_lowest, 'lowest_index': current_lowest_index }) # Update current highest and lowest values and their indices current_highest = num current_highest_index = i current_lowest = float('inf') current_lowest_index = None # if the num is the last value in the list if counter == len(input_list): # Append the pair to the result result_pairs.append({ 'highest_value': current_highest, 'highest_index': current_highest_index, 'lowest_value': current_lowest, 'lowest_index': current_lowest_index }) else: current_highest = num current_highest_index = i return result_pairs def call_find_successive_extremes(): # Example usage with the provided list myList = [10, 12, 18, 20, 25, 18, 17, 16, 10, 20, 30, 35, 40, 35, 30, 20, 15, 17, 19, 21, 23, 25, 32, 30, 29, 6, 19] result = find_successive_extremes(myList) # Display the result for pair in result: print(f"Highest Value: {pair['highest_value']} at Index: {pair['highest_index']}") print(f"Lowest Value: {pair['lowest_value']} at Index: {pair['lowest_index']}") print("------------------------") if __name__ == "__main__": call_find_successive_extremes()
- Output.
Highest Value: 25 at Index: 4 Lowest Value: 10 at Index: 8 ------------------------ Highest Value: 40 at Index: 12 Lowest Value: 15 at Index: 16 ------------------------ Highest Value: 32 at Index: 22 Lowest Value: 6 at Index: 25 ------------------------ Highest Value: 19 at Index: 26 Lowest Value: inf at Index: None ------------------------
2. Explanation.
- This function, `find_successive_extremes`, iterates through the input list and identifies successive pairs of highest and lowest values along with their respective index values.
- The result is a list of dictionaries, each containing information about a pair of values.
- The example usage demonstrates how to apply this function to the provided list, `myList`, and display the results.