To find the row with a particular value in one column and the lowest value in another column within a 2D array, you can use the following steps:
Example code:
def find_row_with_value_and_lowest_in_column(arr, col1_value, col2_index):
# Create list of tuples with row index and values of columns of interest
tuples_list = [(i, arr[i][col2_index]) for i in range(len(arr))]
# Filter list to include only tuples with matching col1 value
filtered_list = [t for t in tuples_list if arr[t[0]][0] == col1_value]
# Sort filtered list by col2 value in ascending order
sorted_list = sorted(filtered_list, key=lambda t: t[1])
# Return first tuple in sorted list (row index with lowest col2 value)
return sorted_list[0][0]
Note: This code assumes that the 2D array is a list of lists, where each inner list represents a row of the array, and that the second column index is provided as an argument (since it was not specified in the question).
Asked: 2022-04-13 11:00:00 +0000
Seen: 4 times
Last updated: Mar 29 '22