为什么我无法反转 python 中的列表列表

发布于 2024-09-25 01:55:40 字数 442 浏览 15 评论 0原文

我想做这样的事情,但这段代码返回 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

悲喜皆因你 2024-10-02 01:55:40

Python 可变容器的 mutator 方法(例如列表的 .reverse 方法)几乎总是返回 None ——少数返回一个有用的值,例如 .pop 方法返回弹出的元素,但要保留的关键概念是这些变异器都不会返回变异的容器:相反,容器会就地变异并且 mutator 方法的返回值不是那个容器。 (这是 CQS 设计原则的应用 - 并不那么狂热,比如说,在 Eiffel 中,这是由 Bertrand Meyer 设计的语言,他也发明了 CQS,但这只是因为在 Python 中“实用性胜过纯粹性,cfr import this;-)。

建立一个列表通常比仅仅花费更多构建一个迭代器,对于绝大多数常见的情况,您想要做的就是对结果进行循环,因此,诸如 reversed 之类的内置函数(以及所有精彩的构建块)在 itertools 模块中)返回迭代器,而不是列表。

但是,如果您有一个迭代器 x 但确实需要等效的,该怎么办? list y?小菜一碟 - 只需执行 y = list(x) 即可创建 list 类型的新实例。 ,你调用类型list——这是一个非常普遍的Python思想,它比我在前两段中指出的非常重要的东西更重要! -)

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

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

请注意,我使用的是列表理解,而不是 map< /code>:根据经验,map 仅应在不需要 lambda 时用作最后的优化来构建它(如果涉及 lambda ,那么 listcomp 不仅像往常一样更清晰,而且无论如何也往往更快!-)。

一旦您成为“过去的 Python 大师”,如果您的分析告诉您此代码是瓶颈,那么您就可以知道尝试替代方案,例如

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

应用负步切片(又名“火星笑脸”)来制作反向副本行,知道它通常比 list(reversed(row)) 方法更快。但是,除非您的代码只能由您自己或至少精通 Python 的人维护,否则使用最简单的“来自第一原理的代码”方法是一个合理的立场,除非分析告诉您踩下踏板。 (我个人认为“火星笑脸”非常重要,足以避免将这种良好的一般哲学应用于这个特定的用例,但是,嘿,理性的人可能会在这个非常具体的点上有所不同!-)。

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;-).

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.

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]

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!-).

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]

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!-).

心的憧憬 2024-10-02 01:55:40

您还可以使用切片来反转单个列表(到位):

>>> a = [1,2,3,4]
>>> a[::-1]
[4, 3, 2, 1]

所以类似:

all_reversed = [lst[::-1] for lst in figure]

...or...

all_reversed = map(lambda x: x[::-1], figure)

...将执行您想要的操作。

You can also use a slice to get the reversal of a single list (not in place):

>>> a = [1,2,3,4]
>>> a[::-1]
[4, 3, 2, 1]

So something like:

all_reversed = [lst[::-1] for lst in figure]

...or...

all_reversed = map(lambda x: x[::-1], figure)

...will do what you want.

眼藏柔 2024-10-02 01:55:40
reversed_lists = [list(reversed(x)) for x in figure]
reversed_lists = [list(reversed(x)) for x in figure]
℡Ms空城旧梦 2024-10-02 01:55:40
map(lambda row: list(reversed(row)), figure)
map(lambda row: list(reversed(row)), figure)
影子的影子 2024-10-02 01:55:40

您也可以简单地

for row in figure:
    row.reverse()

更改每一行。

You can also simply do

for row in figure:
    row.reverse()

to change each row in place.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文