列表理解拆分循环变量 [英] List comprehensions splitting loop variable
问题描述
我试图找出是否有一种方法可以将列表理解的每次迭代的值仅拆分一次,但在输出中使用两次.
作为我要解决的问题的一个示例,我有以下字符串:
a = "1;2;4\n3;4;5"
我想执行以下操作:
>>> [(x.split(";")[1],x.split(";")[2]) for x in a.split("\n") if x.split(",")[1] != 5]
[('2', '4'), ('4', '5')]
无需将运行拆分三遍.所以像这样(显然是无效的语法,但希望足以使消息通过):
[(x[1],x[2]) for x.split(";") in a.split("\n") if x[1] != 5]
在这个问题上,我不正在寻找获取字符串第二和第三列的理想方法.这只是提供具体示例的一种方式.我当然可以为示例使用:
[x.split(";")[1:3] for x in a.split("\n")]
我想到的可能的解决方案:
- 不使用列表理解
- 保留原样
- 使用
csv.DictReader
,为我的列命名,并使用诸如StringIO
之类的名称作为输入.
大多数情况下,这是一种可以使用的好模式,而不是特定的案例,因此很难回答您为什么要这样做"或这是什么意思"这类问题>
更新:在阅读了以下解决方案后,我进行了一些速度测试.而且我在最基本的测试中发现,所提供的解决方案比上面的幼稚解决方案快35%.
您可以使用生成器表达式周围的列表推导:
[(x[1],x[2]) for x in (x.split(";") for x in a.split("\n")) if x[1] != 5]
I am trying to find out if there is a way to split the value of each iteration of a list comprehension only once but use it twice in the output.
As an example of the problem I am trying to solve is, I have the string:
a = "1;2;4\n3;4;5"
And I would like to perform this:
>>> [(x.split(";")[1],x.split(";")[2]) for x in a.split("\n") if x.split(",")[1] != 5]
[('2', '4'), ('4', '5')]
Without the need for running split three times. So something like this (Which is obviously invalid syntax but hopefully is enough to get the message across):
[(x[1],x[2]) for x.split(";") in a.split("\n") if x[1] != 5]
In this question I am not looking for fancy ways to get the 2nd and 3rd column of the string. It is just a way of providing a concrete example. I could for course for the example use:
[x.split(";")[1:3] for x in a.split("\n")]
The possible solutions I have thought of:
- Not use a list comprehension
- Leave it as is
- Use the
csv.DictReader
, name my columns and something likeStringIO
to give it the input.
This is mostly something that would be a nice pattern to be able to use rather than a specific case so its hard to answer the "why do you want to do this" or "what is this for" kind of questions
Update: After being reading the solution below I went and ran some speed tests. And I found in my very basic tests that the solution provided was 35% faster than the naive solution above.
You could use a list comprehension wrapped around a generator expression:
[(x[1],x[2]) for x in (x.split(";") for x in a.split("\n")) if x[1] != 5]
这篇关于列表理解拆分循环变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!