为什么我无法反转 python 中的列表列表
我想做这样的事情,但这段代码返回 None 列表(我认为这是因为 list.reverse() 正在就地反转列表):
map(lambda row: row.reverse(), figure)
我尝试了这个,但反转返回一个迭代器:
map(reversed, figure)
最后我做了类似的事情,这对我有用,但我不知道这是否是正确的解决方案:
def reverse(row):
"""func that reverse a list not in place"""
row.reverse()
return row
map(reverse, figure)
如果有人有我不知道的更好的解决方案,请让我知道
亲切的问候,
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
kind regards,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Python 可变容器的 mutator 方法(例如列表的
.reverse
方法)几乎总是返回None
——少数返回一个有用的值,例如.pop
方法返回弹出的元素,但要保留的关键概念是这些变异器都不会返回变异的容器:相反,容器会就地变异并且 mutator 方法的返回值不是那个容器。 (这是 CQS 设计原则的应用 - 并不那么狂热,比如说,在 Eiffel 中,这是由 Bertrand Meyer 设计的语言,他也发明了 CQS,但这只是因为在 Python 中“实用性胜过纯粹性,cfrimport this
;-)。建立一个列表通常比仅仅花费更多构建一个迭代器,对于绝大多数常见的情况,您想要做的就是对结果进行循环,因此,诸如
reversed
之类的内置函数(以及所有精彩的构建块)在itertools
模块中)返回迭代器,而不是列表。但是,如果您有一个迭代器
x
但确实需要等效的,该怎么办? listy
?小菜一碟 - 只需执行y = list(x)
即可创建list
类型的新实例。 ,你调用类型list
——这是一个非常普遍的Python思想,它比我在前两段中指出的非常重要的东西更重要! -)因此,根据前面段落中的关键概念,针对您的特定问题的代码确实非常容易组合在一起:
请注意,我使用的是列表理解,而不是
map< /code>:根据经验,
map
仅应在不需要lambda
时用作最后的优化来构建它(如果涉及 lambda ,那么 listcomp 不仅像往常一样更清晰,而且无论如何也往往更快!-)。一旦您成为“过去的 Python 大师”,如果您的分析告诉您此代码是瓶颈,那么您就可以知道尝试替代方案,例如
应用负步切片(又名“火星笑脸”)来制作反向副本行,知道它通常比
list(reversed(row))
方法更快。但是,除非您的代码只能由您自己或至少精通 Python 的人维护,否则使用最简单的“来自第一原理的代码”方法是一个合理的立场,除非分析告诉您踩下踏板。 (我个人认为“火星笑脸”非常重要,足以避免将这种良好的一般哲学应用于这个特定的用例,但是,嘿,理性的人可能会在这个非常具体的点上有所不同!-)。The mutator methods of Python's mutable containers (such as the
.reverse
method of lists) almost invariably returnNone
-- 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, cfrimport this
;-).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 theitertools
module) return iterators, not lists.But what if you therefore have an iterator
x
but really truly need the equivalent listy
? Piece of cake -- just doy = list(x)
. To make a new instance of typelist
, you call typelist
-- 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:
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 alambda
to build it (if alambda
is involved then a listcomp, as well as being clearer as usual, also tends to be faster anyway!-).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
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!-).您还可以使用切片来反转单个列表(未到位):
所以类似:
...or...
...将执行您想要的操作。
You can also use a slice to get the reversal of a single list (not in place):
So something like:
...or...
...will do what you want.
您也可以简单地
更改每一行。
You can also simply do
to change each row in place.