简体   繁体   English

大多数Pythonic for /枚举循环?

[英]Most Pythonic for / enumerate loop?

I have a loop condition: 我有一个循环条件:

for count, item in enumerate(contents):

but only want to use the first 10 elements of contents. 但只想使用内容的前10个元素。 What is the most pythonic way to do so? 什么是最蟒蛇的方式呢?

Doing a: 做一个:

if count == 10: break

seems somewhat un-Pythonic. 似乎有些不像Pythonic。 Is there a better way? 有没有更好的办法?

Use a slice (slice notation) 使用切片(切片表示法)

for count, item in enumerate(contents[:10]):

If you are iterating over a generator, or the items in your list are large and you don't want the overhead of creating a new list (as slicing does) you can use islice from the itertools module: 如果您正在迭代生成器,或者列表中的项很大而且您不希望创建新列表的开销(如切片那样),您可以使用itertools模块中的islice

for count, item in enumerate(itertools.islice(contents, 10)):

Either way, I suggest you do this in a robust manner, which means wrapping the functionality inside a function (as one is want to do with such functionality - indeed, it is the reason for the name function) 无论哪种方式,我建议你以一种健壮的方式做到这一点,这意味着将功能包装在一个函数中(因为人们想要使用这样的功能 - 实际上,这是名称功能的原因)

import itertools

def enum_iter_slice(collection, slice):
    return enumerate(itertools.islice(collection, slice))

Example: 例:

>>> enum_iter_slice(xrange(100), 10)
<enumerate object at 0x00000000025595A0>
>>> list(enum_iter_slice(xrange(100), 10))
[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6), (7, 7), (8, 8), (9, 9)]
>>> for idx, item in enum_iter_slice(xrange(100), 10):
    print idx, item

0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9

If you were using enumerate and your count variable just to check the items index (so that using your method you could exit/break the loop on the 10th item.) You don't need enumerate, and just use the itertools.islice() all on its own as your function. 如果你使用enumerate和你的count变量来检查项目索引(这样你可以使用你的方法退出/打破第10项的循环。)你不需要枚举,只需使用itertools.islice()一切都靠自己的功能。

for item in itertools.islice(contents, 10):

If the list is large or contents is a generator, use itertools.islice() : 如果该列表是大或contents是发电机,使用itertools.islice()

from itertools import islice

for count, item in enumerate(islice(contents, 10)):

For smaller lists or tuples, just use slicing: 对于较小的列表或元组,只需使用切片:

for count, item in enumerate(contents[:10]):

Slicing creates a new list or tuple object, which for 10 items is not that big a deal. 切片会创建一个新的列表或元组对象,这对于10个项目来说并不是什么大不了的事。

Just an addendum to the two answers already given: 只是给出了两个答案的附录:

I propose to think about the situation you are in. Why is is that you want to do 10 iterations and no more? 我建议考虑一下你所处的情况。 为什么你要做10次迭代而不再做? Is it more the wish to abort after ten cycles (eg because each cycle takes up a long time and the whole runtime shall be limited while giving up completeness) or is the reason behind your requirement that the first ten elements are a special group of items (maybe the ten to display)? 是否希望在十个周期后中止(例如因为每个周期占用很长时间并且整个运行时间在限制时放弃完整性)或者是你要求前十个元素是一组特殊项目的原因(可能是十个显示)?

In the first case I'd say the most correct way would actually be the if ... break you proposed because it represents your thoughts most direct. 在第一种情况下,我会说最正确的方法实际上是if ... break你的提议,因为它代表了你最直接的想法。

In the latter case I'd propose to name the child, ie introduce a speaking name for that special group of the first ten elements (eg elements_to_display ) and iterate over them. 在后一种情况下,我建议为孩子命名,即为前十个元素(例如elements_to_display )中的特殊组引入一个发音名称并对其进行迭代。

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

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