0

suppose I have the values in 2-d array as:

array = [[0.12],[0.24],[1.24],[1.45],[2.05],[0.45]]

and I have to find the largest value from above, so the result should be only: 2.05 please give me the idea for this. (without iterations if there is precise code then better)

lkkkk
  • 1,999
  • 4
  • 23
  • 29
  • Did you have any ideas in mind yourself? If you tried something and it didnt work, thats ok – Tim Mar 13 '14 at 07:25

2 Answers2

2

Easiest way is:

from itertools import chain

array = [[0.12],[0.24],[1.24],[1.45],[2.05],[0.45]]
value = max(chain.from_iterable(array))
#2.05

A key term you may have been missing to find related posts to this is "flattening" - some useful posts:

How to flatten lists

  1. Flattening a shallow list in Python
  2. Making a flat list out of list of lists in Python
  3. Comprehension for flattening a sequence of sequences?
Community
  • 1
  • 1
Jon Clements
  • 138,671
  • 33
  • 247
  • 280
  • 3
    Not that it is any better, but maybe worth noting you can use `chain` with unpacking of the iterable: `max(chain(*array))` – sberry Mar 13 '14 at 07:27
0

Here is a way without having to import any modules:

>>> array = [[0.12],[0.24],[1.24],[1.45],[2.05],[0.45]]
>>> max([item for sublist in array for item in sublist])
2.05

List comprehensions to the rescue!

anon582847382
  • 19,907
  • 5
  • 54
  • 57