嵌套列表子数组元素索引获取及矩阵等差数列检索函数开发求助
Hey there! Let's break down your two technical challenges and work through practical solutions for each.
1. Finding the index of an element in a nested list/subarray
To get the index of an element in a nested 2D list, you'll need to iterate through both the outer list (tracking row indices) and each inner subarray (tracking column indices). Here's a straightforward Python implementation:
Solution Code
def find_nested_element_index(nested_list, target): # Iterate over each row with its index for row_idx, subarray in enumerate(nested_list): if target in subarray: # Get the column index of the target in the subarray col_idx = subarray.index(target) return (row_idx, col_idx) # Return None if the target isn't found anywhere return None
Notes
- This returns the first occurrence of the target element. If your nested list has duplicate elements and you need all matching indices, use a list comprehension to collect them:
def find_all_nested_indices(nested_list, target): return [ (row_idx, col_idx) for row_idx, subarray in enumerate(nested_list) for col_idx, elem in enumerate(subarray) if elem == target ] - Example usage:
my_nested_list = [[10, 20, 30], [40, 50, 60], [70, 80, 90]] print(find_nested_element_index(my_nested_list, 50)) # Output: (1, 1) print(find_all_nested_indices(my_nested_list, 30)) # Output: [(0, 2)]
2. Implementing the horizontal(m) function to find 4-element arithmetic sequences
Let's focus first on the horizontal direction (same row). The core idea is to check every consecutive group of 4 elements in each row, verifying if they form an arithmetic sequence (i.e., the difference between consecutive elements is constant).
Solution Code
def horizontal(m): matching_sequences = [] rows = len(m) if rows == 0: return matching_sequences for row_idx, row in enumerate(m): row_length = len(row) # We need at least 4 elements to form a sequence if row_length < 4: continue # Iterate through all possible starting columns for 4-element groups for start_col in range(row_length - 3): # Extract the 4 consecutive elements a, b, c, d = row[start_col], row[start_col+1], row[start_col+2], row[start_col+3] # Check if the differences between consecutive elements are equal if (b - a) == (c - b) and (c - b) == (d - c): # Record the positions of the 4 elements sequence_positions = [ (row_idx, start_col), (row_idx, start_col+1), (row_idx, start_col+2), (row_idx, start_col+3) ] matching_sequences.append(sequence_positions) return matching_sequences
Key Details
- This checks consecutive 4 elements (the most common use case for matrix-based sequence problems). If you need to support non-consecutive elements (any 4 elements in a row forming an arithmetic sequence), the logic gets more complex (involves checking all combinations of 4 elements, which is slower for large rows).
- Example test case:
test_matrix = [ [1, 2, 3, 4, 5], # Has two horizontal sequences: [1,2,3,4] and [2,3,4,5] [10, 8, 6, 4, 2], # Has two descending sequences: [10,8,6,4] and [8,6,4,2] [5, 5, 5, 5, 5], # All consecutive groups are valid (constant difference = 0) [1, 3, 5, 7, 9] # Two ascending sequences with difference 2 ] print(horizontal(test_matrix))
Preview for Vertical/Diagonal Support
Once you have the horizontal logic working, extending to vertical and diagonal directions is straightforward:
- Vertical: Iterate over each column, check consecutive 4 elements in the column (similar to horizontal, but swapping row/column iteration).
- Main Diagonal (top-left to bottom-right): For each starting position
(row, col)whererow + 3 < rowsandcol + 3 < cols, check elements(row, col),(row+1, col+1),(row+2, col+2),(row+3, col+3). - Anti-Diagonal (top-right to bottom-left): For each starting position
(row, col)whererow + 3 < rowsandcol - 3 >= 0, check elements(row, col),(row+1, col-1),(row+2, col-2),(row+3, col-3).
内容的提问来源于stack exchange,提问作者user9258239

