File tree 1 file changed +3
-4
lines changed
1 file changed +3
-4
lines changed Original file line number Diff line number Diff line change 41
41
42
42
>> > from itertools import dropwhile
43
43
>> > with open (' /etc/passwd' ) as f:
44
- ... for line in dropwhile(lambda line : line.startswith(' #' ), f):
44
+ ... for line in dropwhile(lambda line : not line.startswith(' #' ), f):
45
45
... print (line, end = ' ' )
46
46
...
47
47
nobody:* :- 2 :- 2 :Unprivileged User:/ var/ empty:/ usr/ bin / false
50
50
>> >
51
51
52
52
这个例子是基于根据某个测试函数跳过开始的元素。
53
- 如果你已经明确知道了要跳过的元素的个数的话 ,那么可以使用 ``itertools.islice() `` 来代替。比如:
53
+ 如果你已经明确知道了要跳过的元素的序号的话 ,那么可以使用 ``itertools.islice() `` 来代替。比如:
54
54
55
55
.. code-block :: python
56
56
59
59
>> > for x in islice(items, 3 , None ):
60
60
... print (x)
61
61
...
62
- 1
63
62
4
64
63
10
65
64
15
66
65
>> >
67
66
68
- 在这个例子中, ``islice() `` 函数最后那个 ``None `` 参数指定了你要获取从第3个到最后的所有元素 ,
67
+ 在这个例子中, ``islice() `` 函数最后那个 ``None `` 参数指定了你要跳过前面3个元素,获取第4个到最后的所有元素 ,
69
68
如果 ``None `` 和3的位置对调,意思就是仅仅获取前三个元素恰恰相反,
70
69
(这个跟切片的相反操作 ``[3:] `` 和 ``[:3] `` 原理是一样的)。
71
70
You can’t perform that action at this time.
0 commit comments