Python for 循环中的列表初始化
如何在 for 循环中初始化列表:
for x, y in zip(list_x, list_y):
x = f(x, y)
不幸的是,即使我想要它,这个循环也不会改变 list_x 。
有没有办法在循环中引用 list_x 的元素?
我意识到我可以使用列表理解,但是当for循环非常复杂时,这很难阅读。
编辑:我的 for
循环有 20 行。您通常会将 20 行放入一个列表推导式中吗?
How do initialize a list in a for loop:
for x, y in zip(list_x, list_y):
x = f(x, y)
unfortunately, this loop does not alter list_x even though I want it to.
Is there a way to have references to the elements of list_x in the loop?
I realize I could use a list comprehension, but that's hard to read when the for loop is very complicated.
Edit: My for
loop is 20 lines. Would you normally put 20 lines into a single list comprehension?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为什么列表理解会很复杂?
您可以使用一组生成器表达式或将代码子集抽象为
f
函数,而不是使用 20 行 for 循环。在看不到代码的情况下讨论可以做什么确实毫无意义。
why would list-comprehension be complicated?
Instead of having 20-line for loop, you could use a set of generator expressions or abstract a subset of code into an
f
function.It's really is pointless to talk about what could be done w/o seeing the code.
这能行吗?
Would this do it?
这也只是一个穷人的冗长列表理解。
This is just a poor man's verbose list comprehension, also.