为什么我不能在python中反转列表列表 [英] why i can't reverse a list of list in python

查看:84
本文介绍了为什么我不能在python中反转列表列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做这样的事情,但是这段代码返回None的列表(我认为是因为list.reverse()正在将列表反转到位):

i wanted to do something like this but this code return list of None (i think it's because list.reverse() is reversing the list in place):

map(lambda row: row.reverse(), figure)

我尝试过这个,但是反向返回一个迭代器:

i tried this one, but the reversed return an iterator :

map(reversed, figure)

最后,我做了这样的事情,它对我有用,但是我不知道这是否是正确的解决方案:

finally i did something like this , which work for me , but i don't know if it's the right solution:

def reverse(row):
    """func that reverse a list not in place"""
    row.reverse()
    return row

map(reverse, figure)

如果有人有我不知道的更好的解决方案,请告诉我

if someone has a better solution that i'm not aware of please let me know

亲切的问候,

推荐答案

Python可变容器的 mutator 方法(例如列表的.reverse方法)几乎总是返回None -一些返回一个有用的值,例如.pop方法返回popped元素,但是要保留的关键概念是,这些变异器均未返回变异容器:相反,容器会原位变异 和mutator方法的返回值不是那个容器. (这是 CQS 设计原则的应用-不像它那么狂热,例如,在埃菲尔(Eiffel)中,贝特朗·迈耶(Bertrand Meyer)发明了该语言,他也发明了CQS,但这仅仅是因为在Python中实用性胜过纯度,cfr import this;-).

The mutator methods of Python's mutable containers (such as the .reverse method of lists) almost invariably return None -- a few return one useful value, e.g. the .pop method returns the popped element, but the key concept to retain is that none of those mutators returns the mutated container: rather, the container mutates in-place and the return value of the mutator method is not that container. (This is an application of the CQS principle of design -- not quite as fanatical as, say, in Eiffel, the language devised by Bertrand Meyer, who also invented CQS, but that's just because in Python "practicality beats purity, cfr import this;-).

构建列表通常比仅构建迭代器更为昂贵,因为在绝大多数情况下,您要做的就是对结果进行 loop .因此,诸如reversed(以及itertools模块中所有出色的构建基块)之类的内置函数会返回迭代器,而不是 列表.

Building a list is often costlier than just building an iterator, for the overwhelmingly common case where all you want to do is loop on the result; therefore, built-ins such as reversed (and all the wonderful building blocks in the itertools module) return iterators, not lists.

但是,如果您因此拥有一个迭代器x但确实确实需要等效的 list y怎么办?小菜一碟-只需y = list(x).要创建类型为list的新实例,您可以 call 类型为list-这是Python的一般概念,与我在本文中指出的重要内容相比,保留它甚至更加重要.前两段!-)

But what if you therefore have an iterator x but really truly need the equivalent list y? Piece of cake -- just do y = list(x). To make a new instance of type list, you call type list -- this is such a general Python idea that it's even more crucial to retain than the pretty-important stuff I pointed out in the first two paragraphs!-)

因此,根据前面几段中的关键概念,针对您特定问题的代码非常容易组合在一起:

So, the code for your specific problem is really very easy to put together based on the crucial notions in the previous paragraphs:

[list(reversed(row)) for row in figure]

请注意,我使用的是列表理解,不是 map:根据经验,map仅应在存在时用作最后的优化不需要lambda来构建它(如果涉及到lambda,则listcomp以及往常一样清晰,无论如何也趋于更快!-).

Note that I'm using a list comprehension, not map: as a rule of thumb, map should only be used as a last-ditch optimization when there is no need for a lambda to build it (if a lambda is involved then a listcomp, as well as being clearer as usual, also tends to be faster anyway!-).

一旦您是"Python的过去的高手",如果您的分析告诉您此代码是瓶颈,那么您就可以知道尝试其他替代方法,例如

Once you're a "past master of Python", if your profiling tells you that this code is a bottleneck, you can then know to try alternatives such as

[row[::-1] for row in figure]

应用负步切片(又称"Martian Smiley")制作行的反向副本,因为它通常比list(reversed(row))方法更快.但是-除非您的代码仅由您自己或至少由Python熟练的人来维护,否则使用最简单的第一原则的代码"方法是一种可取的立场,除非剖析告诉您踩踏板. (我个人认为,"Martian Smiley"非常重要,可以避免将这种良好的通用哲学应用于这种特定的用例,但是,嘿,有理性的人可能在这个非常特定的方面有所不同!!).

applying a negative-step slicing (aka "Martian Smiley") to make reversed copies of the rows, knowing it's usually faster than the list(reversed(row)) approach. But -- unless your code is meant to be maintained only by yourself or somebody at least as skilled at Python -- it's a defensible position to use the simplest "code from first principles" approach except where profiling tells you to push down on the pedal. (Personally I think the "Martian Smiley" is important enough to avoid applying this good general philosophy to this specific use case, but, hey, reasonable people could differ on this very specific point!-).

这篇关于为什么我不能在python中反转列表列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆