Merlin
Merlin

Reputation: 25629

Doing math with Python lists

I have output like this:

LL= [['a', 2, 3, 4, 13], ['b', 6, 7, 8, 13], ['c', 10, 11, 12, 13]]

Instead of "13", I would like to get maximum of elements by "row" using 2, 3 ,4

where ['a', 2, 3, 4, 13] would be 4 ['b', 6, 7, 8, 13], would be 8.

EDIT: I need to replace "13" with the max value.

Then add "14" after 13

for row in LL: row[5:6] = [14]

then replace "14" with a another inter row math.. How can I do this... these are tables but not matrices. Should use Numpy? Ref please so I can look up.

Upvotes: 1

Views: 729

Answers (2)

eat
eat

Reputation: 7530

Is this in the directions what you are looking for?

LL= [['a', 2, 3, 4, 13], ['b', 6, 7, 8, 13], ['c', 10, 11, 12, 13]]

for row in LL:
    row[-1]= max(row[1: -1])
    row.append(14)
print LL

If not please describe more detailed manner your requirements.

Upvotes: 4

Keith Randall
Keith Randall

Reputation: 23265

compute maximum of all but first and last element:

for row in LL:
  print max(row[1:-1])

add 14 to end of each row:

for row in LL:
  row.append(14)

Upvotes: 5

Related Questions