python - 处理包含子列表的父列表(带有变量计数)

Vin*_*eet 2 python

作为输入,我得到一个包含子列表(带有变量计数)的主列表.

masterList = [[23,12],[34,21],[25,20]]
Run Code Online (Sandbox Code Playgroud)

子列表的数量各不相同.此处显示3个子列表,但数量可能会有所不同.
我希望得到最多的第一记录和最小的第二记录.
在这种情况下,我知道我可以像这样硬编码......

maxNum = max(masterList[0][0], masterList[1][0], masterList[2][0])
Run Code Online (Sandbox Code Playgroud)

如何编写一个模块来接受具有不同数量的子列表并获得max,min的masterList?

谢谢.

Aja*_*234 8

你可以使用zip:

masterList = [[23,12],[34,21],[25,20]]
first, second = zip(*masterList)
print(max(first))
print(min(second))
Run Code Online (Sandbox Code Playgroud)

编辑:对于包含两个以上元素的子列表的数据,您可以使用Python3解包来解释其余的元素:

masterList = [[23,12, 24],[34,21, 23],[25,20, 23, 23]]
first, second, *_ = zip(*masterList)
print(max(first))
print(min(second))
Run Code Online (Sandbox Code Playgroud)

输出:

34
12
Run Code Online (Sandbox Code Playgroud)