问题描述
看看以下内容:
>>> 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']
我正在尝试在每个小写字母之前列出一个以字符^
开头的列表.在此示例中,您需要使用sum
展平列表.但是,我的问题是,是否有可能首先列出一个简单的清单.上面的输出是所需的输出.
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. However, my question is, if it is possible to make a flat list in the first place. The output above is the desired output.
同样,在变量中不断添加 ,该变量随for
循环的每次迭代而变化.一个人不能在这里使用两个for
循环,因为这太简单了,例如:
As in, append something constantly before a variable that changes with each iteration of the for
loop. One cannot use two for
loops here, as that would be too simple, for example:
mystring = 'ABCELKJSDLHFWEHSJDHFKHIUEHFSDF'
print [item for x in mystring.lower() for item in ['^', x]]
如果有人这样做:
>>> 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']
您会在列表中找到列表.因此,有没有一种方法可以一次在列表理解中附加两个项目,而不必使用附加的循环或诸如sum
之类的附加功能?我问这个问题,因为它很简单,但我找不到解决方法.如果有人尝试执行以下操作:
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
? 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
尝试给出SyntaxError
.那么,我要问的是用Python做不到的吗?使用()
给我一个元组列表.
The attempt gives a SyntaxError
. So, is what I'm asking impossible to do in Python? Using ()
gives me a list of tuples.
我还使用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 start with this:
print list( '^'.join(mystring.lower()) )
给出:
['a', '^', 'b', '^', 'c', '^', ...]
因此,这将提供所需的输出:
So this would give the desired output:
l = list( '^'.join(mystring.lower()) )
l.insert(0, '^')
print l
另一种方式:
print [ y for x in zip(['^'] * len(mystring), mystring.lower()) for y in x ]
给出:
['^', 'a', '^', 'b', '^', 'c', ...
这篇关于在列表理解中一次添加两个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!