非家庭作业
我有浮动x列表,我想将它转换为list y,这是x中所有第十个元素的列表。
出于我自己的原因,我真的很想用少量的线条来做这件事。我想出了这样的东西:
i = 0
y = filter(lambda x: (++i)%10; x)从理论上讲,这应该是可行的,i已经定义好了,++i通常会将一个添加到变量i中,然后继续执行表达式。
不幸的是,++并不存在于Python中。
有什么毕达通的方法吗?
我的另一个想法是使用一个映射,并将表达式push元素放到list y上。
如果我能说得更清楚的话请告诉我。
发布于 2013-10-08 17:23:58
那[value for index, value in enumerate(list_of_floats) if index % 10 == 0]呢
发布于 2013-10-08 17:35:51
使用itertools.count的替代方案
>>> x = range(1, 101)
>>> i = itertools.count(1)
>>> y = filter(lambda item: next(i) % 10 == 0, x)
>>> y
[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]https://stackoverflow.com/questions/19254250
复制相似问题