按第n项拆分列表 [英] spliting a list by nth items

查看:86
本文介绍了按第n项拆分列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得以前可能已经回答了这个问题,但是我无法在档案中找到类似的东西。如果你知道这已经被解答了,请随意指点我




我有一个特定顺序的列表,我想拆分成两个

列表:每个第n项的列表,以及剩余项目的列表。

维护两个列表中的原始订单非常重要。


所以第一个清单很简单:


nth_items = items [:: n]


第二个列表是给我带来麻烦的。到目前为止,我提出的最好的价格是:


non_nth_items = list(items)

del non_nth_items [:: n]


这是正确的方法吗?


史蒂夫

-

如果你只是动词,你可以将任何字面化。

- Bucky Katt,Get Fuzzy

解决方案

< blockquote> Steven Bethard写道:

我觉得这可能之前已经得到了解答,但我无法在档案中找到类似的东西。如果你知道这已经得到了解答,请随时指点我


我有一个特定顺序的列表,我想分成两个列表:每个第n项的清单,以及剩余项目的清单。
在两个清单中维护原始订单非常重要。

所以第一个清单很简单:

nth_items = items [:: n]

第二个清单是给我带来麻烦的。到目前为止,我提出的最好的是:

non_nth_items = list(items)
del non_nth_items [:: n]
这是正确的方法吗?




可能没有正确的办法。我认为,你的方式不是我想要做的事情。但它很棒。 :-)

(也很简单明了。)


-Peter


Steven Bethard写道:

我有一个特定顺序的列表,我想分成两个
列表:每个第n项的列表,以及剩余项目的列表。保持两个列表中的原始顺序非常重要。

所以第一个列表很简单:

nth_items = items [:: n]

第二个清单是给我带来麻烦的。




non_nth_items = [如果是x%n,x为x项目中的x]

-

Michael Hoffman


我相信你的意思是:


non_nth_items = [items [i-1] for i in range(1,len(items)-1)if i%n]


我不认为if x%n不能在他的物品清单上工作


拉里贝茨


"迈克尔霍夫曼" < m。********************************* @ example.com>写在

消息新闻:ci ********** @ pegasus.csx.cam.ac.uk ...

Steven Bethard写道:

我有一个特定顺序的列表,我想分成两个
列表:每个第n个项目的列表,以及剩余项目的列表。保持两个列表中的原始顺序非常重要。

所以第一个列表很简单:

nth_items = items [:: n]

第二个清单是给我带来麻烦的。



non_nth_items = [如果是x%n,x为x项目中的x]
-
Michael Hoffman



I feel like this has probably been answered before, but I couldn''t
find something quite like it in the archives. Feel free to point me
somewhere if you know where this has already been answered.

I have a list in a particular order that I want to split into two
lists: the list of every nth item, and the list of remaining items.
It''s important to maintain the original order in both lists.

So the first list is simple:

nth_items = items[::n]

The second list is what''s giving me trouble. So far, the best I''ve
come up with is:

non_nth_items = list(items)
del non_nth_items[::n]

Is this the right way to do this?

Steve
--
You can wordify anything if you just verb it.
- Bucky Katt, Get Fuzzy

解决方案

Steven Bethard wrote:

I feel like this has probably been answered before, but I couldn''t
find something quite like it in the archives. Feel free to point me
somewhere if you know where this has already been answered.

I have a list in a particular order that I want to split into two
lists: the list of every nth item, and the list of remaining items.
It''s important to maintain the original order in both lists.

So the first list is simple:

nth_items = items[::n]

The second list is what''s giving me trouble. So far, the best I''ve
come up with is:

non_nth_items = list(items)
del non_nth_items[::n]

Is this the right way to do this?



There''s probably no "right" way. Your way isn''t the way I
would have thought to do it, I think. But it''s brilliant. :-)
(Also simple and clear.)

-Peter


Steven Bethard wrote:

I have a list in a particular order that I want to split into two
lists: the list of every nth item, and the list of remaining items.
It''s important to maintain the original order in both lists.

So the first list is simple:

nth_items = items[::n]

The second list is what''s giving me trouble.



non_nth_items = [x for x in items if x % n]
--
Michael Hoffman


I believe you meant somthing like:

non_nth_items = [items[i-1] for i in range(1,len(items)-1) if i % n]

I don''t think the "if x % n" won''t work on his list of items

Larry Bates

"Michael Hoffman" <m.*********************************@example.com > wrote in
message news:ci**********@pegasus.csx.cam.ac.uk...

Steven Bethard wrote:

I have a list in a particular order that I want to split into two
lists: the list of every nth item, and the list of remaining items. It''s
important to maintain the original order in both lists.

So the first list is simple:

nth_items = items[::n]

The second list is what''s giving me trouble.



non_nth_items = [x for x in items if x % n]
--
Michael Hoffman



这篇关于按第n项拆分列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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