重复列表中的元素

时间:2013-11-28 01:43:58

标签: python list for-loop

所以这是我的清单:

my_list = [['a','b','c'],['d','e','f']]

我希望每次输出的次数为2次:

final_list = [['a','a','b','b','c','c'],['d','d','e','e','f','f']]

这就是我在做的事情:

final_list = []
for new_list in my_list:
    for my_new_list in new_list:
        for i in range(2):
             final_list.append(my_new_list)

但它显示:

final_list = ['a','a','b','b','c','c','d','d','e','e','f','f']

我该如何解决?顺便说一句,我希望用for循环来做。谢谢

2 个答案:

答案 0 :(得分:2)

这只适用于解析数组数组,我认为这应该足够了:

final_list = []
for sublist in my_list:
    temp_list = []
    for item in sublist:
        temp_list += [item] * 2
    final_list.append(temp_list)

答案 1 :(得分:1)

def change(L):
    res = []
    for i in L:
        temp = []
        for j in i:
            temp.append(j)
            temp.append(j)
        res.append(list)
    return res