在列表理解中一次添加两个项目 [英] Adding two items at a time in a list comprehension

查看:68
本文介绍了在列表理解中一次添加两个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一次将两个项目添加到列表理解中.一项是恒定的.如何仅使用列表理解内的一个for循环而不使用其他功能来实现此目的.不使用任何导入的答案将受到青睐.

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. Answers that do not use any import will be favored.

看看以下内容:

>>> 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', ...

这篇关于在列表理解中一次添加两个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆