python - 如何在列表中返回列表的元素?

标签 python

所以我本质上有一个 2 维和 1/2 维矩阵,我想从列表中的列表返回一个元素。

def somefunc(x):
    # What I want to do is return the max element within a matrix based on l[:][1]
    return j

# example
l = [[[1,2,3],4],
    [[5,6,7],8],
    [[9,1,2],3]]

>>>somefunc(l)
[[5,6,7],8]

最佳答案

max() 函数中使用 key 参数。

def somefunc(x):
    return max(x, key=lambda e: e[1])

关于python - 如何在列表中返回列表的元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41856742/

相关文章:

python - 堆叠 pandas DataFrame 时设置列名

python - windows 7 上的 pil png activestate ZLIB (PNG/ZIP) 支持不可用

python - 如何在 Python 中 .write() 两个项目,一个零和一个迭代器?

python - 将字典传递给具有 base_estimator 特征的 sklearn 分类器

python - 在 docker-py 中发布到随机端口并返回端口

python - 定时器在 Python 中停止后无法重新启动

python - 使用 pypyodbc 运行多个存储过程给出不完整的结果

python - 使用 URL 路径或查询参数进行分页

python - chrome 浏览器缺少 Django 2.0 CSS

python - 如何在Python中创建全局变量