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
data=[
['8', '6', '7'],
['5', '0', '8'],
['2', '12', '4'],
['0', '7', '9']
]
print([max(r,key=int)for rindata])
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])
© 2024 OneMinuteCode. All rights reserved.