[英]Adding two items at a time in a list comprehension
I want to add two items at a time to a list comprehension.
我想一次向列表理解中添加两个项目。 One item is constant.
一项是不变的。 How can this be achieved using only one for loop inside of a list comprehension, and no additional functions.
如何在列表推导式中仅使用一个 for 循环而不使用其他函数来实现这一点。 Answers that do not use any import will be favored.
不使用任何导入的答案将受到青睐。
Take a look at at the following:看看以下内容:
>>> mystring = 'ABCELKJSDLHFWEHSJDHFKHIUEHFSDF'
>>> sum([['^', char] for char in mystring.lower()], [])
['^', 'a', '^', 'b', '^', 'c', '^', 'e', '^', 'l', '^', 'k', '^', 'j', '^', 's', '^', 'd', '^', 'l', '^', 'h', '^', 'f', '^', 'w', '^', 'e', '^', 'h', '^', 's', '^', 'j', '^', 'd', '^', 'h', '^', 'f', '^', 'k', '^', 'h', '^', 'i', '^', 'u', '^', 'e', '^', 'h', '^', 'f', '^', 's', '^', 'd', '^', 'f']
I am trying to make a list with the character ^
prepended before each letter in lower case.我正在尝试在每个小写字母前加上字符
^
来制作一个列表。 In this example, you need to use sum
to flatten the list.在本例中,您需要使用
sum
来展平列表。 However, my question is, if it is possible to make a flat list in the first place.但是,我的问题是,是否可以首先制作一个平面列表。 The output above is the desired output.
上面的输出是所需的输出。
As in, append something constantly before a variable that changes with each iteration of the for
loop.就像在随着
for
循环的每次迭代而变化的变量之前不断添加一些东西。 One cannot use two for
loops here, as that would be too simple, for example:在这里不能使用两个
for
循环,因为这太简单了,例如:
mystring = 'ABCELKJSDLHFWEHSJDHFKHIUEHFSDF'
print [item for x in mystring.lower() for item in ['^', x]]
If one does something like this:如果有人做这样的事情:
>>> mystring = 'ABCELKJSDLHFWEHSJDHFKHIUEHFSDF'
>>> [['^', x] for x in mystring]
['^', 'a', '^', 'b', '^', 'c', '^', 'e', '^', 'l', '^', 'k', '^', 'j', '^', 's', '^', 'd', '^', 'l', '^', 'h', '^', 'f', '^', 'w', '^', 'e', '^', 'h', '^', 's', '^', 'j', '^', 'd', '^', 'h', '^', 'f', '^', 'k', '^', 'h', '^', 'i', '^', 'u', '^', 'e', '^', 'h', '^', 'f', '^', 's', '^', 'd', '^', 'f']
You get lists within lists.您可以在列表中获取列表。 Thus, is there a way that you can append two items at a time in a list comprehension without have to use an addition for loop or an additional function like
sum
?因此,有没有一种方法可以在列表推导式中一次附加两个项目,而不必使用循环加法或
sum
类的附加函数? I ask this, because its something quite simple, yet I can't find a way to do it.我问这个,因为它很简单,但我找不到办法做到这一点。 If one tries to do the following:
如果尝试执行以下操作:
>>> ['^', x for x in mystring.lower()]
File "<console>", line 1
['^', x for x in mystring.lower()]
^
SyntaxError: invalid syntax
The attempt gives a SyntaxError
.该尝试给出了
SyntaxError
。 So, is what I'm asking impossible to do in Python?那么,我所要求的在 Python 中是不可能做到的吗? Using
()
gives me a list of tuples.使用
()
给我一个元组列表。
I've also tried using the splat
/ unpacking
operator:我也试过使用
splat
/ unpacking
操作符:
>>> [*['^', x] for x in mystring.lower()]
File "<console>", line 1
[*['^', x] for x in mystring.lower()]
^
SyntaxError: invalid syntax
But as above, this too is a syntax error.但如上所述,这也是一个语法错误。 Sorry for this late edit, but I have tried the following:
很抱歉这么晚的编辑,但我已经试过如下:
import itertools
mystring = "HELLOWORLD"
print(list(itertools.chain.from_iterable(('^', x) for x in mystring.lower())))
But the above still required an import.但以上仍然需要导入。
You can use itertools.chain.from_iterable
, this is equivalent to that nested list comprehension version but slightly efficient(for large lists):您可以使用
itertools.chain.from_iterable
,这相当于嵌套列表理解版本,但效率稍高(对于大型列表):
>>> from itertools import chain
>>> mystring = 'ABCELKJSDLHFWEHSJDHFKHIUEHFSDF'
>>> list(chain.from_iterable([['^', x] for x in mystring]))
['^', 'A', '^', 'B', '^', 'C', '^', 'E', '^', 'L', '^', 'K', '^', 'J', '^', 'S', '^', 'D', '^', 'L', '^', 'H', '^', 'F', '^', 'W', '^', 'E', '^', 'H', '^', 'S', '^', 'J', '^', 'D', '^', 'H', '^', 'F', '^', 'K', '^', 'H', '^', 'I', '^', 'U', '^', 'E', '^', 'H', '^', 'F', '^', 'S', '^', 'D', '^', 'F']
In Python 3.3+ you can also use yield from
in a generator function:在 Python 3.3+ 中,您还可以在生成器函数中使用
yield from
:
>>> mystring = 'ABCELKJSDLHFWEHSJDHFKHIUEHFSDF'
>>> def solve(strs):
... for x in strs:
... yield from ['^', x]
...
>>> list(solve(mystring))
['^', 'A', '^', 'B', '^', 'C', '^', 'E', '^', 'L', '^', 'K', '^', 'J', '^', 'S', '^', 'D', '^', 'L', '^', 'H', '^', 'F', '^', 'W', '^', 'E', '^', 'H', '^', 'S', '^', 'J', '^', 'D', '^', 'H', '^', 'F', '^', 'K', '^', 'H', '^', 'I', '^', 'U', '^', 'E', '^', 'H', '^', 'F', '^', 'S', '^', 'D', '^', 'F']
You can start with this:你可以从这个开始:
print list( '^'.join(mystring.lower()) )
which gives:这给出:
['a', '^', 'b', '^', 'c', '^', ...]
So this would give the desired output:所以这将给出所需的输出:
l = list( '^'.join(mystring.lower()) )
l.insert(0, '^')
print l
And another way:而另一种方式:
print [ y for x in zip(['^'] * len(mystring), mystring.lower()) for y in x ]
which gives:这给出:
['^', 'a', '^', 'b', '^', 'c', ...
If anyone's using pydash, you can use flatten_deep .如果有人使用 pydash,您可以使用flatten_deep 。 Basically, it recursively flattens your list down to one level so you can do a list comprehension of multiple lists.
基本上,它递归地将您的列表展平到一个级别,以便您可以对多个列表进行列表理解。
>>> import pydash
>>> mystring = 'ABCELKJSDLHFWEHSJDHFKHIUEHFSDF'
>>> pydash.flatten_deep([['^', x] for x in mystring])
['^', 'A', '^', 'B', '^', 'C', '^', 'E', '^', 'L', '^', 'K', '^', 'J', '^', 'S', '^', 'D', '^', 'L', '^', 'H', '^', 'F', '^', 'W', '^', 'E', '^', 'H', '^', 'S', '^', 'J', '^', 'D', '^', 'H', '^', 'F', '^', 'K', '^', 'H', '^', 'I', '^', 'U', '^', 'E', '^', 'H', '^', 'F', '^', 'S', '^', 'D', '^', 'F']
Well, in this specific case...嗯,在这个特定的情况下......
>>> list('abcd'.replace('', '^')[ : -1])
['^', 'a', '^', 'b', '^', 'c', '^', 'd']
>>>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.