如何应用两个变量的列表理解

发布于 2025-01-30 18:55:21 字数 612 浏览 3 评论 0原文

我试图了解列表的理解,以便可以在正在从事更有效的项目上制定代码。我找到了一个易于理解的简单示例:

L = []
for i in range (1, 10):
    if i%3 == 0:
        L.append (i)
print(L)

[3,6,9]

,这是具有列表理解的代码:

L = [i for i in range (1, 10) if i%3 == 0]
print(L)

我试图将我刚刚学到的内容应用于此代码,但是我很难弄清楚如何做那。

L = []
M = []
for i in range (1, 10):
    if i%3 == 0:
        L.append (i)
        M.append(i*2)
print(L,M)

[3,6,9] [6,12,18]

以下内容无效,但说明了我在寻找的内容:

[L,M] = [i for i in range (1, 10) if i%3 == 0, i*2 for i in range (1, 10) if i%3 == 0]

I am trying to understand list comprehention so that I can make the code on a project I'm working on more efficient. I found a simple example which is easy to understand:

L = []
for i in range (1, 10):
    if i%3 == 0:
        L.append (i)
print(L)

[3, 6, 9]

And this is the code with list comprehension:

L = [i for i in range (1, 10) if i%3 == 0]
print(L)

I tried to apply what I have just learned to this code but I'm having trouble figuring out how to do that.

L = []
M = []
for i in range (1, 10):
    if i%3 == 0:
        L.append (i)
        M.append(i*2)
print(L,M)

[3, 6, 9] [6, 12, 18]

The following is not valid Python, but illustrates what I'm looking for:

[L,M] = [i for i in range (1, 10) if i%3 == 0, i*2 for i in range (1, 10) if i%3 == 0]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

木森分化 2025-02-06 18:55:21

您要查找的列表理解的语法比您想象的要简单得多:

x = [(i, i*2) for i in range (1, 1000) if i%3 == 0]
print(x)

现在是最后一个位置的位置:l,m

您需要的是此问题

因此,例如:

L, M = zip(*x)

另请参见此 link 显示如何在循环和列表理解之间进行转换。

The syntax of the list comprehension that you are looking for is far simpler than you imagine:

x = [(i, i*2) for i in range (1, 1000) if i%3 == 0]
print(x)

Now for the last bit where you are after two lists: L,M.

What you need are the answers to this question

So, for example:

L, M = zip(*x)

Also see this link which dynamically shows how to transform between for loop and list comprehension.

套路撩心 2025-02-06 18:55:21

您需要2个列表综合,其中之一将产生l和另一个m。您几乎在那里,除了几个括号

[L, M] = [[i for i in range (1, 10) if i%3 == 0], [i*2 for i in range (1, 10) if i%3 == 0]]

实际上,您无需将lm放入列表中 - 这可以正常工作

L, M = [[i for i in range (1, 10) if i%3 == 0], [i*2 for i in range (1, 10) if i%3 == 0]]
print(L)
# [3, 6, 9]
print(M)
# [6, 12, 18]

You want 2 list comprehensions, one of which produces L and the other M. You are almost there, except for a couple of brackets

[L, M] = [[i for i in range (1, 10) if i%3 == 0], [i*2 for i in range (1, 10) if i%3 == 0]]

In fact, you don't need to put L and M in a list - this works just fine

L, M = [[i for i in range (1, 10) if i%3 == 0], [i*2 for i in range (1, 10) if i%3 == 0]]
print(L)
# [3, 6, 9]
print(M)
# [6, 12, 18]
我不吻晚风 2025-02-06 18:55:21

谢谢您的建议!他们俩都帮助我解决了我的脑海中的结,所以只能将绿色支票提供给其中一个是不公平的。

起初, mortz'代码对我来说很有意义,因为它非常接近我的“解决方案”。经过一番处理后,我也理解了Quamranas 解决方案,起初似乎更复杂。

这样写下来,紧随其后的事情有助于我理解。这对许多人来说似乎很微不足道,但是也许它可以帮助像我一样慢的人。

x = [(i, i*2) for i in range (1, 10) if i%3 == 0]
#i = 3
#x = [(3,6)]
#i = 6
#x = [(3,6),(6,12)]
#i = 9
#x = [(3,6),(6,12),(9,18)]
print(x)
L,M = zip(*x)
print(L,M)
#(3, 6, 9) (6, 12, 18)

Thank you for your suggestions! They both helped me to resolve the knot in my head, so it's a bit unfair to have to give the green check to only one of them.

At first, Mortz' code made a lot of sense to me because it was very close to my 'solution'. After some crunching, I understood quamranas solution too, which seemed more complicated at first.

Writing it down like this and following closely what happens helped me understand. It may seem trivial to many, but maybe it helps someone who is as slow as me.

x = [(i, i*2) for i in range (1, 10) if i%3 == 0]
#i = 3
#x = [(3,6)]
#i = 6
#x = [(3,6),(6,12)]
#i = 9
#x = [(3,6),(6,12),(9,18)]
print(x)
L,M = zip(*x)
print(L,M)
#(3, 6, 9) (6, 12, 18)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文