-3

Let's say I have the following nested list of (Strings)

[['78130 00821', '98453 94494', '01-09-2016 06:01:12', '186'],    
['78298 91466', '(022)28952819', '01-09-2016 06:01:59', '2093'],    
['97424 22395', '(022)47410783', '01-09-2016 06:03:51', '1975'],    
['93427 40118', '(080)33118033', '01-09-2016 06:11:23', '1156'],    
['90087 42537', '(080)35121497', '01-09-2016 06:17:26', '573']]
  1. I want to compare the 4th element in each sub-list with the others : Ex: I want to compare 186, 2093..573 and print the maximum value of each element.
  2. I want to print the 2nd element of the sublist who's 4th element is the highest.
Chris
  • 29,127
  • 3
  • 28
  • 51

3 Answers3

1
data = [['78130 00821', '98453 94494', '01-09-2016 06:01:12', '186'],
['78298 91466', '(022)28952819', '01-09-2016 06:01:59', '2093'],
['97424 22395', '(022)47410783', '01-09-2016 06:03:51', '1975'],
['93427 40118', '(080)33118033', '01-09-2016 06:11:23', '1156'],
['90087 42537', '(080)35121497', '01-09-2016 06:17:26', '573']]


print (max(data, key=lambda x: int(x[3]))[1])

output:

(022)28952819

with basic for loop:

max = 0
for item in data:
    if int(item[3]) > max:
        max = int(item[3])
        result = item[1]

print (result)

output:

(022)28952819
ncica
  • 7,015
  • 1
  • 15
  • 37
  • if you can notice, the time of posting answer is almost the same, so at the time of writing my answer I did not see his ;) – ncica Jul 24 '19 at 07:22
  • You had improved your answer, now it's useful for user not used to lambda ! – Dorian Turba Jul 24 '19 at 07:24
  • 1
    tnx :) I was thinking about some basic code also,because obviously the OP is the beginner :) – ncica Jul 24 '19 at 07:26
  • 1
    @DorianTurba, I just thought the same. But indeed, my post was also very shortly after the answer of Sunitha, and almost exactly the same. It just confirms that this is a very Pythonic way to solve this problem. ncica added the "traditional" for loop for those less familiar with Python, which adds something to the answer and is fine by me :-) – wovano Jul 24 '19 at 11:00
  • PS: The latter solution (using for-loop) does not work if the last column only contains negative numbers. So, in general, I would initialize with the value of the first row instead of with 0. – wovano Jul 24 '19 at 11:02
0

You can use max with a key to indicate that it should use the fourth element

>>> l = [['78130 00821', '98453 94494', '01-09-2016 06:01:12', '186'],    
... ['78298 91466', '(022)28952819', '01-09-2016 06:01:59', '2093'],    
... ['97424 22395', '(022)47410783', '01-09-2016 06:03:51', '1975'],    
... ['93427 40118', '(080)33118033', '01-09-2016 06:11:23', '1156'],    
... ['90087 42537', '(080)35121497', '01-09-2016 06:17:26', '573']]
>>> 
>>> max(l, key=lambda sl: int(sl[3]))
['78298 91466', '(022)28952819', '01-09-2016 06:01:59', '2093']
>>> max(l, key=lambda sl: int(sl[3]))[1]
'(022)28952819'
Sunitha
  • 11,777
  • 2
  • 20
  • 23
0
data = [['78130 00821', '98453 94494', '01-09-2016 06:01:12', '186'],    
['78298 91466', '(022)28952819', '01-09-2016 06:01:59', '2093'],    
['97424 22395', '(022)47410783', '01-09-2016 06:03:51', '1975'],    
['93427 40118', '(080)33118033', '01-09-2016 06:11:23', '1156'],    
['90087 42537', '(080)35121497', '01-09-2016 06:17:26', '573']]

result = max(data, key=lambda x: int(x[3]))[1]

result:

'(022)28952819'
wovano
  • 4,543
  • 5
  • 22
  • 49