我如何比较和打印 python 中的 2D 列表中的元素?

我有这个 2D 列表,其中第一个元素是公司名称,第二个元素是高级开发人员的薪水,第三个元素是他们每周工作的小时数。

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

我正在尝试比较brands[0][1] with brand[1][1] and brands [2][1]和打印行业中的最低和最高工资,例如"The lowest wage: Google, 110" "The highest wage: Apple,150",然后打印最低和最高工作时间,对于一个简短的列表,if 和 else 语句很容易,但我正在尝试为一个通用循环更大的名单。

我试过 min() 但没有成功,我相信有办法让它工作。


拉丁的传说
浏览 91回答 3
3回答

catspeake

也许这会奏效: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])

繁华开满天机

您可以按所需的类别使用lambda进行排序,将参考工资,然后打印相应的指数,即我们的低点和高点 key=lambda x: x[1]list[0][0], list[0][x]list[-1][0], list[-1][x]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, 110The highest wage: Apple, 150The lowest working hours: Google, 35The highest working hours: Apple, 40
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python