Ask Your Question
0

What is the row with a particular value in one column and the lowest value in another column within a 2D array?

asked 2022-04-13 11:00:00 +0000

ladyg gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2022-03-29 09:00:00 +0000

woof gravatar image

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:

  1. Create a list of tuples where each tuple contains the row index and the value of the two columns of interest.
  2. Filter the list of tuples to include only those tuples where the value of the first column matches the particular value you are looking for.
  3. Sort the filtered list of tuples based on the second column in ascending order.
  4. Return the first tuple in the sorted list, which represents the row with the particular value in the first column and the lowest value in the second column.

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).

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-04-13 11:00:00 +0000

Seen: 4 times

Last updated: Mar 29 '22