在Python中返回多维的最小/最大?
我有一个列表,其形式为
[ [[a,b,c],[d,e,f]] , [[a,b,c],[d,e,f]] , [[a,b,c],[d,e,f]] ... ] etc.
我想返回最小 c 值和最大 c+f 值。这可能吗?
I have a list in the form of
[ [[a,b,c],[d,e,f]] , [[a,b,c],[d,e,f]] , [[a,b,c],[d,e,f]] ... ] etc.
I want to return the minimal c value and the maximal c+f value. Is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
对于最小
c
:对于最大
c+f
示例:
For the minimum
c
:For the maximum
c+f
Example:
列表理解来救援
List comprehension to the rescue
您必须将您的列表映射到仅包含您关心的项目的列表。
这是一种可能的方法:
min
和max
调用的内容就是所谓的 "generator",负责生成原始数据到过滤后数据的映射。You have to map your list to one containing just the items you care about.
Here is one possible way of doing this:
The contents of the
min
andmax
calls is what is called a "generator", and is responsible for generating a mapping of the original data to the filtered data.当然有可能。您有一个列表,其中包含一个二元素列表的列表,这些列表本身就是列表。你的基本算法是
Of course it's possible. You've got a list containing a list of two-element lists that turn out to be lists themselves. Your basic algorithm is
假设您的列表存储在 my_list 中:
suppose your list is stored in my_list: