复制列表中的每个成员 [英] Duplicate each member in a list

查看:39
本文介绍了复制列表中的每个成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个恢复列表[1,5,3,6,...]的函数 并给出[1,1,5,5,3,3,6,6,...]. 知道怎么做吗?

I want to write a function that revives a list [1,5,3,6,...] and gives [1,1,5,5,3,3,6,6,...]. Any idea how to do it?

推荐答案

>>> a = range(10)
>>> [val for val in a for _ in (0, 1)]
[0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9]

传统上,_用作占位符变量名称,在该位置您不希望对变量的内容做任何事情.在这种情况下,它仅用于每次在外循环中生成两个值.

N.B. _ is traditionally used as a placeholder variable name where you do not want to do anything with the contents of the variable. In this case it is just used to generate two values for every time round the outer loop.

要将其从列表转换为生成器,请用圆括号替换方括号.

To turn this from a list into a generator replace the square brackets with round brackets.

这篇关于复制列表中的每个成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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