0

I have this 2D list where the first element is the name of company the second is how much a senior developer gets salary and the third element is how many hours they work/week.

brands = [["Microsoft", "120", "38", "1124"], ["Apple", "150", "40", "1800"], ["Google", "110", "35", "1437"]]

I am trying to compare brands[0][1] with brand[1][1] and brands [2][1] and print the lowest and highest wages in industry e.g "The lowest wage: Google, 110" "The highest wage: Apple,150", then printing lowest and highest working hours, for a short list it is ease with if and else statements but i am trying to make a general loop for in case of a much larger list.

I tried min() but it was unsuccessfully, i am sure there is a way to make it work though.

rapidDev
  • 109
  • 2
  • 13
Sachihiro
  • 1,597
  • 2
  • 20
  • 46

4 Answers4

3

I would sort your list, then take the first and last element. You can put it in an f-string for printing (if using python 3.6 or above):

sorted_brands = sorted(brands, key=lambda x: int(x[1]))

print(f'the lowest wage: {sorted_brands[0][0]}, {sorted_brands[0][1]}, the highest wage: {sorted_brands[-1][0]}, {sorted_brands[-1][1]}')
#'the lowest wage: Google, 110, the highest wage: Apple, 150'
sacuL
  • 49,704
  • 8
  • 81
  • 106
  • What does `key=lambda and x:int(x[1])` do, if you can explain? – Sachihiro Oct 03 '18 at 22:36
  • It says: sort the lists by the value of `x[1]`, cast as an integer. `x` is an anonymous variable (you can read about [lambda functions here](https://stackoverflow.com/q/16501/6671176)) – sacuL Oct 03 '18 at 22:37
1

Maybe this will work:

wage=[]
for var in range(len(brands)):
   wage.append(brands[var][1])

min=wage.index(min(wage))
print("The lowest wage :{0}, {1}".format(brands[min][0],brands[min][1])
nikhilbisht21
  • 63
  • 2
  • 7
  • 1
    This works too, this was my initial idea to collect the values in a new list and then print the lowest value with min() method but did not know how, because i am in early beginning in coding. Thanks – Sachihiro Oct 03 '18 at 22:46
1

You can sort with lambda by the desired category key=lambda x: x[1], would refer to wages, Then print the corresponding indices, of the list[0][0], list[0][x] for our low and list[-1][0], list[-1][x] for our highs

wage = sorted(brands, key=lambda x: x[1])
print('The lowest wage: {}, {}'.format(wage[0][0], wage[0][1]))
print('The highest wage: {}, {}'.format(wage[-1][0], wage[-1][1]))

hours = (sorted(brands, key=lambda x: x[2]))
print('The lowest working hours: {}, {}'.format(hours[0][0], hours[0][2]))
print('The highest working hours: {}, {}'.format(hours[-1][0], hours[-1][2]))
The lowest wage: Google, 110
The highest wage: Apple, 150
The lowest working hours: Google, 35
The highest working hours: Apple, 40
vash_the_stampede
  • 4,590
  • 1
  • 8
  • 20
  • Thanks man, @sacul did exactly the same thing. But i will save this code for the next project because this time i am not allowed to lambda, however i appreciate it. – Sachihiro Oct 03 '18 at 22:52
0
company = None
minwage = None

for brand in brands:
    if minwage is None or brand[1] < minwage:
        minwage = brand[1]
        company = brand[0]

print 'Lowest wage was: %s,%s' % (minwage, company)
John Gordon
  • 29,573
  • 7
  • 33
  • 58