Python learning notes (12) Python files and iterations (2)

Keywords: Python Big Data

iteration

Basic meaning

Iteration is the activity of repeating the feedback process, whose purpose is usually to approach and reach the desired goal or result. Each iteration of the process is called an "iteration", and the result of each iteration will be used as the initial value of the next iteration.

In computational science, iteration is the repetition of a set of instructions (or steps) in a program. It can be used either as a general term (synonymous with "Repetition") or as a description of a particular form of repetition with variable states.

operation

 1 >>> lst =[1,2,3,4]
 2 >>> for i in lst:  #for loop reads every element in the list
 3 ...     print i
 4 ...
 5 1
 6 2
 7 3
 8 4
 9 >>> lit =iter(lst) #Create iterator object
10 >>> type(lit) #List iterator
11 <type 'listiterator'>
12 >>> dir(lit) #Method to view list iterators, next()
13 ['__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__iter__', '__length_hint__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'next']
14 >>> lit.next() First element in list element
15 1
16 >>> lit.next() Second element in list element
17 2
18 >>> lit.next()
19 3
20 >>> lit.next()
21 4
22 >>> lit.next()
23 Traceback (most recent call last):
24   File "<stdin>", line 1, in <module>
25 StopIteration
26 >>> lit =iter(lst)
  #this for Cycle and front list for The result of the cycle is the same. In fact, there are differences in the operation process.
  Similar to files, when reading big data, use for Loop completion of the process of reading each element in the iterator object can show the advantages of the iterator.
  Because it is to take the elements to be read one by one into memory, rather than put them all into memory
27 >>> for i in lit: 28 ... print i 29 ... 30 1 31 2 32 3 33 4 34 >>> help(iter) 35 Help on built-in function iter in module __builtin__: 36 #The return value is an iteration object
  #Parameter must be an object conforming to iteration protocol or a sequence object 37 iter(...) 38 iter(collection) -> iterator 39 iter(callable, sentinel) -> iterator 40 41 Get an iterator from an object. In the first form, the argument must 42 supply its own iterator, or be a sequence. 43 In the second form, the callable is called until it returns the sentinel. 44 45 >>>

Iteration is one of Python's most powerful functions and a way to access collection elements.

An iterator is an object that remembers the location of the traversal.

Iterator objects are accessed from the first element of the collection until all elements are accessed. The iterator can only move forward and not backward.

Iterators have two basic methods: iter() and next().

String, list, or tuple objects can be used to create iterators

Iterator objects can be traversed using regular for statements

You can also use the next() function

Posted by fael097 on Sat, 04 Apr 2020 17:12:06 -0700