列表上的连锁操作(Chainable operations on lists)

编程入门 行业动态 更新时间:2024-10-24 06:26:49
表上的连锁操作(Chainable operations on lists)

我想要做类似的事情

list = [6,4,1,10,8].sort().reverse()

要么

zip([x**2 for x in xrange(5)], [x**3 for x in xrange(5)].reverse()])

但这不起作用,因为列表中的方法不会返回原始对象,所以它们不可链接。

我是否必须使用多行执行此操作,或者是否有一些很酷的函数式编程魔法,我可以在python中通过一行来实现这一点?

I want to do something like

list = [6,4,1,10,8].sort().reverse()

or

zip([x**2 for x in xrange(5)], [x**3 for x in xrange(5)].reverse()])

but this doesn't work because methods on list doesn't return the original object, so they're not chainable.

Do I have to do this using multiple lines or is there some cool functional programming magic I can do in python to achieve this in one line?

最满意答案

你想sorted()和reversed() 。

You want sorted() and reversed().

更多推荐

本文发布于:2023-08-07 16:13:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465109.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表上   操作   Chainable   operations   lists

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!