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

标签 python

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

masterList = [[23,12],[34,21],[25,20]]

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

maxNum = max(masterList[0][0], masterList[1][0], masterList[2][0])

如何编写一个模块来接受具有不同数量子列表的 masterList 并获取最大值、最小值?

谢谢。

最佳答案

你可以使用zip:

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

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

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

输出:

34
12

关于python - 处理包含子列表的父列表(带有变量计数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49777436/

相关文章:

python - 获取字典映射值到引用 ID

python - Cython动态库链接

python - pip install pyodbc 失败错误 : Failed building wheel for pyodbc

python - 在pytorch中计算困惑度

python - 根据列值返回SQL Server列名和对应的值

python - 自动获取django中 "static"文件夹路径

python: xpath 从 boxofficemojo.com 返回空列表

python - 如何以 fortran 中连续零可以写为 n*0 的格式声明数组?

python - 根据最小订单选择独特的产品

python - 尝试从 nytimes.com 下载 .pdf .png 和 .jpg