"崩"列表中的更改列表 [英] "Collapsing" a list into a list of changes

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

问题描述

大家好,


我有一个连续重复值的项目列表,但

重复的次数和位置并不重要,所以我只需要
需要剥离它们。例如,如果我的原始列表是

[0,0,1,1,1,2,2,3,3,3,2,2,2,4,4,4,5 ],我想以[0,1,2,3,2,4,5]结束。


这就是我现在这样做的方式:


def straightforward_collapse(myList):

collapsed = [myList [0]]

for my in myList [1:]:

如果n!=折叠[-1]:

collapsed.append(n)


返回已折叠


有没有一种优雅的方法可以做到这一点,或者我应该坚持上面的代码




谢谢,

Alan McIntyre
http://www.esrgtech.com

Hi all,

I have a list of items that has contiguous repetitions of values, but
the number and location of the repetitions is not important, so I just
need to strip them out. For example, if my original list is
[0,0,1,1,1,2,2,3,3,3,2,2,2,4,4,4,5], I want to end up with [0,1,2,3,2,4,5].

Here is the way I''m doing this now:

def straightforward_collapse(myList):
collapsed = [myList[0]]
for n in myList[1:]:
if n != collapsed[-1]:
collapsed.append(n)

return collapsed

Is there an elegant way to do this, or should I just stick with the code
above?

Thanks,
Alan McIntyre
http://www.esrgtech.com

推荐答案

Alan McIntyre写道:
Alan McIntyre wrote:
大家好,

我有一份清单具有连续重复值的项目,但重复的数量和位置并不重要,所以我只需要将它们剥离出来。例如,如果我的原始列表是[0,0,1,1,1,2,2,3,3,3,2,2,2,4,4,4,5],我想要结束[0,1,2,3,2,4,5]。

这就是我现在这样做的方式:

def straightforward_collapse (myList):
折叠= [myList [0]]
for myList [1:]:
如果n!=折叠[-1]:
collapsed.append (n)

返回已折叠

有没有一种优雅的方法可以做到这一点,或者我应该坚持使用上面的代码?
Hi all,

I have a list of items that has contiguous repetitions of values, but
the number and location of the repetitions is not important, so I just
need to strip them out. For example, if my original list is
[0,0,1,1,1,2,2,3,3,3,2,2,2,4,4,4,5], I want to end up with [0,1,2,3,2,4,5].

Here is the way I''m doing this now:

def straightforward_collapse(myList):
collapsed = [myList[0]]
for n in myList[1:]:
if n != collapsed[-1]:
collapsed.append(n)

return collapsed

Is there an elegant way to do this, or should I just stick with the code
above?




好​​吧,这也是关于同样的事情,但是使用枚举和列表

理解:


py> lst = [0,0,1,1,1,2,2,3,3,3,2,2,2,4,4,4,5]

py> [项目为i,项目为枚举(lst),如果i == 0或项目!= lst [i-1]]

[0,1,2,3,2,4,5]


类似的代码,每次通过时都不会检查''如果我== 0'':


py> itr = enumerate(lst)

py> itr.next()

(0,0)

py> [lst [0]] + [项目为i,项目在itr项目!= lst [i-1]]

[0,1,2,3,2,4,5]


我不知道其中任何一个是否真的更优雅......


Steve



Well, this does about the same thing, but using enumerate and a list
comprehension:

py> lst = [0,0,1,1,1,2,2,3,3,3,2,2,2,4,4,4,5]
py> [item for i, item in enumerate(lst) if i == 0 or item != lst[i-1]]
[0, 1, 2, 3, 2, 4, 5]

Similar code that doesn''t check ''if i == 0'' each time through:

py> itr = enumerate(lst)
py> itr.next()
(0, 0)
py> [lst[0]] + [item for i, item in itr if item != lst[i-1]]
[0, 1, 2, 3, 2, 4, 5]

I don''t know if either of these is really more elegant though...

Steve


2005年2月4日星期五12:43:37 -0500,Alan McIntyre写道:
On Fri, 04 Feb 2005 12:43:37 -0500, Alan McIntyre wrote:
def straightforward_collapse(myList):
collapsed = [myList [0] ]
for myList [1:]:
如果n!=折叠[-1]:
collapsed.append(n)

返回折叠

是否有一种优雅的方式来做到这一点,或者我应该坚持上面的代码?
def straightforward_collapse(myList):
collapsed = [myList[0]]
for n in myList[1:]:
if n != collapsed[-1]:
collapsed.append(n)

return collapsed

Is there an elegant way to do this, or should I just stick with the code
above?




我认为''非常优雅;我读了它,并立即明白了你在做什么

。如果你对大型列表执行此操作,可能会进行一些性能调整,如果你使用它,我的直觉就是把它重写为

迭代器很像:


for collapse中的项目(yourList):


但除了可能不适用的项目之外,直截了当

一般都是*好*的东西,你不觉得吗? :-)



I think that''s pretty elegant; I read it and immediately understood what
you were doing. There may be some performance tweaks you could make if you
were doing this to large lists, and my instincts say to re-write it as an
iterator if you use it a lot like:

for item in collapse(yourList):

but other than that which may not even apply, "straightforward" is
generally a *good* thing, don''t you think? :-)


2005年2月4日星期五下午12:43:37 -0500,Alan McIntyre写道:
On Fri, Feb 04, 2005 at 12:43:37PM -0500, Alan McIntyre wrote:
大家好,

我有一系列具有连续重复值的项目,但是重复的数量和位置并不重要,所以我只需要去除他们出去了。例如,如果我的原始列表是[0,0,1,1,1,2,2,3,3,3,2,2,2,4,4,4,5],我想要结束[0,1,2,3,2,4,5]。

这就是我现在这样做的方式:

def straightforward_collapse (myList):
折叠= [myList [0]]
for myList [1:]:
如果n!=折叠[-1]:
collapsed.append (n)

返回已折叠

有没有一种优雅的方法可以做到这一点,或者我应该坚持使用上面的代码?
Hi all,

I have a list of items that has contiguous repetitions of values, but
the number and location of the repetitions is not important, so I just
need to strip them out. For example, if my original list is
[0,0,1,1,1,2,2,3,3,3,2,2,2,4,4,4,5], I want to end up with [0,1,2,3,2,4,5].

Here is the way I''m doing this now:

def straightforward_collapse(myList):
collapsed = [myList[0]]
for n in myList[1:]:
if n != collapsed[-1]:
collapsed.append(n)

return collapsed

Is there an elegant way to do this, or should I just stick with the code
above?



如果你使用python2.4,


If you are using python2.4,

import itertools
[x]为(x)in ...groupby([0,0,1,1,1,2,2,3,3,3,2,2,2,4,4,4,5]) ]
[0,1,2,3,2,4,5]


因为这是2.4你也可以返回一个生成器表达式。

def iter_collapse(myList):
.... return(x [0] for(x)in it.groupby([0,0,1,1,1,2,2,3,3, 3,2,2,2,4,4,4,5]))

.... i = iter_c ollapse([0,0,1,1,1,2,2,3,3,3,2,2,2,4,4,4,5])
i
< generator对象在0xb7df6b2c> list(i)
[0,1,2,3,2,4,5]
import itertools as it
[x[0] for (x) in it.groupby([0,0,1,1,1,2,2,3,3,3,2,2,2,4,4,4,5])] [0, 1, 2, 3, 2, 4, 5]
Since this is 2.4 you could also return a generator expression.
def iter_collapse(myList): .... return (x[0] for (x) in it.groupby([0,0,1,1,1,2,2,3,3,3,2,2,2,4,4,4,5]))
.... i = iter_collapse([0,0,1,1,1,2,2,3,3,3,2,2,2,4,4,4,5])
i <generator object at 0xb7df6b2c> list(i) [0, 1, 2, 3, 2, 4, 5]



-Jack


-Jack


这篇关于&QUOT;崩&QUOT;列表中的更改列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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