I want to list the 0th value of each element in a two-dimensional array

Asked 2 years ago, Updated 2 years ago, 20 views

This is Python.

 data=[
    ['8',  '6', '7'],
    ['5',  '0', '8'],
    ['2', '12', '4'],
    ['0',  '7', '9']
]

In response to this data,

 ['8', '8', '12', '9']

I would like to print this out, but I can't get the last element, and the previous element will be printed.

Below is the program.

defind_max(data, begin, end):      
     for a in data [begin-1]:
         data1 = sorted (data[begin-1], reverse = True)
                    
         begin + = 1
         result=data1[0]
         print(result)
     return result

python

2022-09-30 12:09

2 Answers

 data=[
    ['8',  '6', '7'],
    ['5',  '0', '8'],
    ['2', '12', '4'],
    ['0',  '7', '9']
]

print([max(r,key=int)for rindata])


2022-09-30 12:09

I don't think the begin or end parameters are required for the intended action to be inferred from what is written in the question.

begin and end are remnants of the previous question, and are they not necessary for this question?
Two-dimensional array row/column extraction

"If you want to ""extract and list the maximum values for each row in the specified 2D array"" that is inferred from the question, you can do this."

 data=[
    ['8',  '6', '7'],
    ['5',  '0', '8'],
    ['2', '12', '4'],
    ['0',  '7', '9']
]

data = [str(max(map(int,row))))) for row in data ]

If you compare the string (with 1 origin), the maximum value of the third line is 4, so we convert it to int before processing it.

@metropolis's answer was more concise.
I didn't notice that python sometimes combines various functions.
max(iterable,*[,key,default])


2022-09-30 12:09

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.