元素出现两次的列表理解

mar*_*tin 3 python list-comprehension list

假设我有列表

l = ['a', 'c', 'b']
Run Code Online (Sandbox Code Playgroud)

我想要的是一个列表,其中这些元素一个接一个出现两次,所以

['a', 'a', 'c', 'c', 'b', 'b']
Run Code Online (Sandbox Code Playgroud)

我想以最 Pythonic 的方式做到这一点。

我的一半解决方案是做类似的事情

[[l[i], l[i]] for i in range(len(l))]
Run Code Online (Sandbox Code Playgroud)

这产生

[['a', 'a'], ['c', 'c'], ['b', 'b']]
Run Code Online (Sandbox Code Playgroud)

从这里开始,我必须解析(遍历)列表以删除内部列表并获得单个平面列表。

有没有人有更好的主意一次性做到这一点?显然,像l * 2这样的事情无济于事['a', 'c', 'b', 'a', 'c', 'b'],我想要相邻的相同元素。

UpS*_*ler 5

l_2 = [item for item in l for i in range(n)]
Run Code Online (Sandbox Code Playgroud)

链接到原点:Stackoverflow:重复列表的元素 n 次