Reputation: 1505
I have an array like this:
elements=[['1', '1', '2'], ['2', '2', '3'], ['3', '3', '4'], ['4', '4', '5'], ['5', '5', '6'], ['6', '6', '7'], ['7', '7', '8'], ['8', '8', '9'], ['9', '9', '10'], ['10', '10', '11'], ['11', '11', '12'], ['12', '12', '13'], ['13', '13', '14'], ['14', '14', '15'], ['15', '15', '16'], ['16', '16', '17'], ['17', '17', '18'], ['18', '18', '19'], ['19', '19', '20'], ['20', '20', '21'], ['21', '21', '22'], ['22', '22', '23'], ['23', '23', '24'], ['24', '24', '25'], ['25', '25', '26'], ['26', '26', '27'], ['27', '27', '28'], ['28', '28', '29'], ['29', '29', '30'], ['30', '30', '31'], ['31', '32', '33'], ['32', '33', '34'], ['33', '34', '35'], ['34', '35', '36'], ['35', '36', '37'], ['36', '37', '38'], ['37', '38', '39'], ['38', '39', '40'], ['39', '40', '41'], ['40', '41', '42'], ['41', '42', '43'], ['42', '43', '44'], ['43', '44', '45'], ['44', '45', '46'], ['45', '46', '47'], ['46', '47', '48'], ['47', '48', '49'], ['48', '49', '50'], ['49', '50', '51'], ['50', '51', '52'], ['51', '52', '53'], ['52', '53', '54'], ['53', '54', '55'], ['54', '55', '56'], ['55', '56', '57'], ['56', '57', '58'], ['57', '58', '59'], ['58', '59', '60'], ['59', '60', '61'], ['60', '61', '62'], ['61', '63', '64'], ['62', '64', '65'], ['63', '65', '66'], ['64', '66', '67'], ['65', '67', '68'], ['66', '68', '69'], ['67', '69', '70'], ['68', '70', '71'], ['69', '71', '72'], ['70', '72', '73'], ['71', '73', '74'], ['72', '74', '75'], ['73', '75', '76'], ['74', '76', '77'], ['75', '77', '78'], ['76', '78', '79'], ['77', '79', '80'], ['78', '80', '81'], ['79', '81', '82'], ['80', '82', '83'], ['81', '83', '84'], ['82', '84', '85'], ['83', '85', '86'], ['84', '86', '87'], ['85', '87', '88'], ['86', '88', '89'], ['87', '89', '90'], ['88', '90', '91'], ['89', '91', '92'], ['90', '92', '93'], ['91', '94', '95'], ['92', '95', '96'], ['93', '96', '97'], ['94', '97', '98'], ['95', '98', '99'], ['96', '99', '100'], ['97', '100', '101'], ['98', '101', '102'], ['99', '102', '103'], ['100', '103', '104'], ['101', '104', '105'], ['102', '105', '106'], ['103', '106', '107'], ['104', '107', '108'], ['105', '108', '109'], ['106', '109', '110'], ['107', '110', '111'], ['108', '111', '112'], ['109', '112', '113'], ['110', '113', '114'], ['111', '114', '115'], ['112', '115', '116'], ['113', '116', '117'], ['114', '117', '118'], ['115', '118', '119'], ['116', '119', '120'], ['117', '120', '121'], ['118', '121', '122'], ['119', '122', '123'], ['120', '123', '124'], ['121', '125', '126'], ['122', '126', '127'], ['123', '127', '128'], ['124', '128', '129'], ['125', '129', '130'], ['126', '130', '131'], ['127', '131', '132'], ['128', '132', '133'], ['129', '133', '134'], ['130', '134', '135'], ['131', '135', '136'], ['132', '136', '137'], ['133', '137', '138'], ['134', '138', '139'], ['135', '139', '140'], ['136', '141', '142'], ['137', '142', '143'], ['138', '143', '144'], ['139', '144', '145'], ['140', '145', '146'], ['141', '146', '147'], ['142', '147', '148'], ['143', '148', '149'], ['144', '149', '150'], ['145', '150', '151'], ['146', '151', '152'], ['147', '152', '153'], ['148', '153', '154'], ['149', '154', '155'], ['150', '155', '156'], ['151', '157', '158'], ['152', '158', '159'], ['153', '159', '160'], ['154', '160', '161'], ['155', '161', '162'], ['156', '162', '163'], ['157', '163', '164'], ['158', '164', '165'], ['159', '165', '166'], ['160', '166', '167'], ['161', '167', '168'], ['162', '168', '169'], ['163', '169', '170'], ['164', '170', '171'], ['165', '171', '172'], ['166', '172', '173'], ['167', '173', '174'], ['168', '174', '175'], ['169', '175', '176'], ['170', '176', '177'], ['171', '177', '178'], ['172', '178', '179'], ['173', '179', '180'], ['174', '180', '181'], ['175', '181', '182'], ['176', '182', '183'], ['177', '183', '184'], ['178', '184', '185'], ['179', '185', '186'], ['180', '186', '187'], ['181', '188', '189'], ['182', '189', '190'], ['183', '190', '191'], ['184', '191', '192'], ['185', '192', '193'], ['186', '193', '194'], ['187', '194', '195'], ['188', '195', '196'], ['189', '196', '197'], ['190', '197', '198'], ['191', '198', '199'], ['192', '199', '200'], ['193', '200', '201'], ['194', '201', '202'], ['195', '202', '203'], ['196', '203', '204'], ['197', '204', '205'], ['198', '205', '206'], ['199', '206', '207'], ['200', '207', '208'], ['201', '208', '209'], ['202', '209', '210'], ['203', '210', '211'], ['204', '211', '212'], ['205', '212', '213'], ['206', '213', '214'], ['207', '214', '215'], ['208', '215', '216'], ['209', '216', '217'], ['210', '217', '218'], ['211', '219', '220'], ['212', '220', '221'], ['213', '221', '222'], ['214', '222', '223'], ['215', '223', '224'], ['216', '224', '225'], ['217', '225', '226'], ['218', '226', '227'], ['219', '227', '228'], ['220', '228', '229'], ['221', '229', '230'], ['222', '230', '231'], ['223', '231', '232'], ['224', '232', '233'], ['225', '233', '234'], ['226', '235', '236'], ['227', '236', '237'], ['228', '237', '238'], ['229', '238', '239'], ['230', '239', '240'], ['231', '240', '241'], ['232', '241', '242'], ['233', '242', '243'], ['234', '243', '244'], ['235', '244', '245'], ['236', '245', '246'], ['237', '246', '247'], ['238', '247', '248'], ['239', '248', '249'], ['240', '249', '250'], ['241', '251', '252'], ['242', '252', '253'], ['243', '253', '254'], ['244', '254', '255'], ['245', '255', '256'], ['246', '256', '257'], ['247', '257', '258'], ['248', '258', '259'], ['249', '259', '260'], ['250', '260', '261'], ['251', '261', '262'], ['252', '262', '263'], ['253', '263', '264'], ['254', '264', '265'], ['255', '265', '266'], ['256', '267', '268'], ['257', '268', '269'], ['258', '269', '270'], ['259', '270', '271'], ['260', '271', '272'], ['261', '272', '273'], ['262', '273', '274'], ['263', '274', '275'], ['264', '275', '276'], ['265', '276', '277'], ['266', '277', '278'], ['267', '278', '279'], ['268', '279', '280'], ['269', '280', '281'], ['270', '281', '282'], ['271', '283', '284'], ['272', '284', '285'], ['273', '285', '286'], ['274', '286', '287'], ['275', '287', '288'], ['276', '288', '289'], ['277', '289', '290'], ['278', '290', '291'], ['279', '291', '292'], ['280', '292', '293'], ['281', '293', '294'], ['282', '294', '295'], ['283', '295', '296'], ['284', '296', '297'], ['285', '297', '298'], ['286', '298', '299'], ['287', '299', '300'], ['288', '300', '301'], ['289', '301', '302'], ['290', '302', '303'], ['291', '303', '304'], ['292', '304', '305'], ['293', '305', '306'], ['294', '306', '307'], ['295', '307', '308'], ['296', '308', '309'], ['297', '309', '310'], ['298', '310', '311'], ['299', '311', '312'], ['300', '312', '313'], ['301', '314', '315'], ['302', '315', '316'], ['303', '316', '317'], ['304', '317', '318'], ['305', '318', '319'], ['306', '319', '320'], ['307', '320', '321'], ['308', '321', '322'], ['309', '322', '323'], ['310', '323', '324'], ['311', '324', '325'], ['312', '325', '326'], ['313', '326', '327'], ['314', '327', '328'], ['315', '328', '329'], ['316', '329', '330'], ['317', '330', '331'], ['318', '331', '332'], ['319', '332', '333'], ['320', '333', '334'], ['321', '334', '335'], ['322', '335', '336'], ['323', '336', '337'], ['324', '337', '338'], ['325', '338', '339'], ['326', '339', '340'], ['327', '340', '341'], ['328', '341', '342'], ['329', '342', '343'], ['330', '343', '344'], ['331', '345', '346'], ['332', '346', '347'], ['333', '347', '348'], ['334', '348', '349'], ['335', '349', '350'], ['336', '350', '351'], ['337', '351', '352'], ['338', '352', '353'], ['339', '353', '354'], ['340', '354', '355'], ['341', '355', '356'], ['342', '356', '357'], ['343', '357', '358'], ['344', '358', '359'], ['345', '359', '360'], ['346', '361', '362'], ['347', '362', '363'], ['348', '363', '364'], ['349', '364', '365'], ['350', '365', '366'], ['351', '366', '367'], ['352', '367', '368'], ['353', '368', '369'], ['354', '369', '370'], ['355', '370', '371'], ['356', '371', '372'], ['357', '372', '373'], ['358', '373', '374'], ['359', '374', '375'], ['360', '375', '376']]
I'd like to know the maximum value of the first column of this array. However I've found that problematic since I'm not used to python.
I've tried:
import numpy as np
a = np.array(elements)
numEL = a[np.argmax(a)][0]
but I get a wrong result. I know it is 360 but it returns 285...
Any ideas?
Upvotes: 6
Views: 22007
Reputation: 3624
import numpy as np
elements = np.array([['1', '1', '2'], ['10002', '2', '3'], ['360', '10', '11']])
print np.max([int(e) for e in elements[:, 0]])
or
elements = [['1', '1', '2'], ['10002', '2', '3'], ['360', '10', '11']]
elements = np.array([[int(e) for e in element] for element in elements])
print np.max(elements[:, 0])
The first snippet is more efficient since string to int conversion is done only for one column rather than doing type conversion for entire numpy array (as in 2nd snippet).
Hope it helps.
Upvotes: 0
Reputation: 353604
Your elements are strings, and therefore the comparisons are lexicographic. You want to work with integers:
>>> elements=[['1', '1', '2'], ['99', '2', '3'],['360', '10', '11']]
>>> a = np.array(elements,dtype=int)
>>> a
array([[ 1, 1, 2],
[ 99, 2, 3],
[360, 10, 11]])
>>> a.max(axis=0)
array([360, 10, 11])
>>> a.max(axis=0)[0]
360
or simply
>>> a[:,0]
array([ 1, 99, 360])
>>> a[:,0].max()
360
[Note: this is how to get it to work. As for why the code was returning a strange answer in the first place, @mgilson explains that in the comments to this answer.]
Upvotes: 11
Reputation: 17188
maxEl = max(element[0] for element in elements)
Bear in mind that you're still comparing strings here! If you're trying to compare by integer, you need to make sure that you actually do that.
maxIntEl = max(int(element[0]) for element in elements)
Upvotes: 1
Reputation: 62968
In [3]: max(elements, key=lambda e: int(e[0]))
Out[3]: ['360', '10', '11']
Upvotes: 3
Reputation: 464
max(elements, key=lambda x: x[0])
or max(elements, key=lambda x: x[0])[0]
if you just care about the actual value of the first column and not the element that contains the maximum value
Upvotes: 1