简体   繁体   English

复制列表中的项目

[英]Duplicating Items within a list

I am fairly new to python and am trying to figure out how to duplicate items within a list. 我是python的新手,我想弄清楚如何在列表中复制项目。 I have tried several different things and searched for the answer extensively but always come up with an answer of how to remove duplicate items, and I feel like I am missing something that should be fairly apparent. 我已经尝试了几个不同的东西,并广泛搜索答案,但总是想出一个如何删除重复项目的答案,我觉得我错过了一些应该相当明显的东西。

I want a list of items to duplicate such as if the list was [1, 4, 7, 10] to be [1, 1, 4, 4, 7, 7, 10, 10] 我想要一个要复制的项目列表,例如列表是[1,4,7,10]是[1,1,4,4,7,7,10,10]

I know that 我知道

list = range(5)
for i in range(len(list)):
    list.insert(i+i, i)
print list

will return [0, 0, 1, 1, 2, 2, 3, 3, 4, 4] but this does not work if the items are not in order. 将返回[0,0,1,1,2,2,3,3,4,4],但如果项目不按顺序,则不起作用。 To provide more context I am working with audio as a list, attempting to make the audio slower. 为了提供更多上下文,我正在使用音频作为列表,试图使音频变慢。

I am working with: 我正在与:

def slower():
     left = Audio.getLeft()
     right = Audio.getRight()
     for i in range(len(left)):
          left.insert(????)
          right.insert(????)

Where "left" returns a list of items that are the "sounds" in the left headphone and "right" is a list of items that are sounds in the right headphone. 其中“left”返回左耳机中“声音”的项目列表,“right”是右耳机中声音项目的列表。 Any help would be appreciated. 任何帮助,将不胜感激。 Thanks. 谢谢。

Here is a simple way: 这是一个简单的方法:

def slower(audio):
    return [audio[i//2] for i in range(0,len(audio)*2)]

Something like this works: 像这样的东西有效:

>>> list = [1, 32, -45, 12]
>>> for i in range(len(list)):
...     list.insert(2*i+1, list[2*i])
... 
>>> list
[1, 1, 32, 32, -45, -45, 12, 12]

A few notes: 几点说明:

  • Don't use list as a variable name. 不要将list用作变量名。
  • It's probably cleaner to flatten the list zipped with itself. 将自己压缩的列表展平可能更干净。

eg 例如

>>> zip(list,list)
[(1, 1), (-1, -1), (32, 32), (42, 42)]
>>> [x for y in zip(list, list) for x in y]
[1, 1, -1, -1, 32, 32, 42, 42]

Or, you can do this whole thing lazily with itertools : 或者,您可以使用itertools懒洋洋地完成这一切:

from itertools import izip, chain
for item in chain.from_iterable(izip(list, list)):
    print item

I actually like this method best of all. 我最喜欢这种方法。 When I look at the code, it is the one that I immediately know what it is doing (although others may have different opinions on that). 当我查看代码时,我立即知道它正在做什么(尽管其他人可能对此有不同的看法)。

I suppose while I'm at it, I'll just point out that we can do the same thing as above with a generator function: 我想,当我在这里时,我会指出我们可以使用生成器函数执行与上面相同的操作:

def multiply_elements(iterable, ntimes=2):
    for item in iterable:
        for _ in xrange(ntimes):
            yield item

And lets face it -- Generators are just a lot of fun. 让我们面对现实 - 发电机很有趣。 :-) :-)

listOld = [1,4,7,10]
listNew = []

for element in listOld:
    listNew.extend([element,element])

This might not be the fastest way but it is pretty compact 这可能不是最快的方式,但它非常紧凑

a = range(5)
list(reduce(operator.add, zip(a,a)))

a then contains a然后包含

[0, 0, 1, 1, 2, 2, 3, 3, 4, 4]
a = [0,1,2,3]
list(reduce(lambda x,y: x + y, zip(a,a))) #=> [0,0,1,1,2,2,3,3]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM