如何两次附加到列表? [英] how to append to a list twice?

查看:71
本文介绍了如何两次附加到列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我想创建一个表单[100,99,99,98,98,97,97 ...]

(其中每个项目在第一个项目后重复两次) ),我怎么能最有效地做?



现在我有这个:


series = [100]

for x in range(10):#just for testing

series.append(series [-1] - 1)

>
但当然只有一次,而且我不想复制

并粘贴追加线。也许有比这更好的方法。


谢谢。

If I want to create a list of the form [100, 99, 99, 98, 98, 97, 97...]
(where each item is repeated twice after the first one), how might I do
that most efficiently?

Right now I have this:

series = [100]
for x in range(10): # just for testing
series.append(series[-1] - 1)

But of course that only does it once, and I don''t want to have to copy
and paste the append line. Perhaps there''s a better way than this.

Thanks.

推荐答案

" John Salerno" <乔****** @ NOSPAMgmail.com>在留言中写道

news:ui ****************** @ news.tufts.edu ...
"John Salerno" <jo******@NOSPAMgmail.com> wrote in message
news:ui******************@news.tufts.edu...
如果我想创建一个表单[100,99,99,98,98,97,97 ......]
(其中每个项目在第一个项目后重复两次),我该怎么办?最有效的?

现在我有这个:

series = [100]
对于范围内的x(10):#just for testing
series.append(series [-1] - 1)

但当然只做了一次,我不想复制
并粘贴追加线。也许还有比这更好的方法。

谢谢。
If I want to create a list of the form [100, 99, 99, 98, 98, 97, 97...]
(where each item is repeated twice after the first one), how might I do
that most efficiently?

Right now I have this:

series = [100]
for x in range(10): # just for testing
series.append(series[-1] - 1)

But of course that only does it once, and I don''t want to have to copy
and paste the append line. Perhaps there''s a better way than this.

Thanks.




series = [100]

for x在范围(10)中:#仅用于测试

series.extend([series [-1] - 1] * 2)



series = [100]
for x in range(10): # just for testing
series.extend([series[-1] - 1]*2)




John Salerno写道:

John Salerno wrote:
如果我想创建一个表单[100,99,99,98,98,97,97 ...]的列表/>(每个项目在第一个项目后重复两次),我怎么做才能最有效?

现在我有这个:

系列= [100]
对于范围内的x(10):#仅用于测试
series.append(series [-1] - 1)

但当然只有它曾经,我不想复制
并粘贴追加线。也许还有比这更好的方法。

谢谢。
If I want to create a list of the form [100, 99, 99, 98, 98, 97, 97...]
(where each item is repeated twice after the first one), how might I do
that most efficiently?

Right now I have this:

series = [100]
for x in range(10): # just for testing
series.append(series[-1] - 1)

But of course that only does it once, and I don''t want to have to copy
and paste the append line. Perhaps there''s a better way than this.

Thanks.




series = [100]


for i in range(1,10):

series.extend([100-i] * 2)


print series


[100,99,99,98,98,97,97,96,96,95,95,94,94,93,93,92,92,

91,91]


Gerard



series = [100]

for i in range(1,10):
series.extend([100-i]*2)

print series

[100, 99, 99, 98, 98, 97, 97, 96, 96, 95, 95, 94, 94, 93, 93, 92, 92,
91, 91]

Gerard


John Salerno< jo ****** @ NOSPAMgmail.com>写道:
John Salerno <jo******@NOSPAMgmail.com> wrote:
如果我想创建一个表单列表[100,99,99,98,98,97,97 ...]
(每个项目的位置)在第一个之后重复两次),我怎么能最有效地做?

现在我有这个:

series = [100]
for x in range(10):#just for testing
series.append(series [-1] - 1)

但当然只有一次,我不喜欢我不想复制
并粘贴追加线。也许还有比这更好的方法。
If I want to create a list of the form [100, 99, 99, 98, 98, 97, 97...]
(where each item is repeated twice after the first one), how might I do
that most efficiently?

Right now I have this:

series = [100]
for x in range(10): # just for testing
series.append(series[-1] - 1)

But of course that only does it once, and I don''t want to have to copy
and paste the append line. Perhaps there''s a better way than this.




def makeseries(N):

series = [N]

append = series.append

for the tailer in xrange(N-1,-1,-1):

append(tailer)

追加(tailer)

返回系列


series = makeseries(100)


假设通过&最有效的你的意思是最快,这可能会来

关闭;你也想要另外一个替代方案,其中makeseries是一个

生成器,只生成值,最后一个赋值变为

series = list(makeseries(100) )。

Alex



def makeseries(N):
series = [N]
append = series.append
for tailer in xrange(N-1, -1, -1):
append(tailer)
append(tailer)
return series

series = makeseries(100)

assuming that by "most efficiently" you mean "fastest", this might come
close; you''ll want to also time an alternative where makeseries is a
generator which just yields the values, and the last assignment becomes
series=list(makeseries(100)).
Alex


这篇关于如何两次附加到列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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