Python iterable objects and iterators

Python iterator related knowledge points

1. Iterable objects

It is a data set with many private methods, flexible operations (such as addition, deletion, modification and search of lists, dictionaries, common operation methods of strings, etc.), which is relatively intuitive, but takes up memory, and cannot directly obtain values ​​through loop iteration.

  • Objects : Everything in Python is an object.
  • Iterable : cyclic update, not repetition, new content replaces the previous generation content.
  • Iterable objects in Python : Objects that contain the __iter__ method internally are iterable objects.
  • The iterable objects we learned about earlier are : str, list, tuple, dict, set, range
  • The value of an iterable object can be retrieved repeatedly and is not affected by the last read position.
  • Method to determine whether an object is an iterable object : just determine __iter__whether it is in the method list of the object
name = 'amwkvi'
print('__iter__' in dir(name))		# dir()方法将某个对象所拥有的所有方法以列表形式返回。
>>>True
  • Advantages of iterable objects :
    • The stored data can be displayed directly, which is more intuitive.
    • There are many ways to have it.
  • Disadvantages of iterable objects :
    • It takes up more memory.
    • It cannot be traversed directly through a for loop, and the value (index, key) cannot be directly obtained.

2.Iterator

It is a very memory-saving data set that can record the value position and directly obtain the value through the loop + next method, but it is not intuitive and the operation method is relatively simple.

Definition : tool: tool; iterator: tool that can update iteration.

Professional description : An object that contains __iter__methods and __next__methods inside is an iterator.

When the iterator reads the data in the object, it will read downwards from the last read position and cannot return.

An example to illustrate the judgment process : Only one of the iterators learned so far “文件句柄”is an iterator.

with open('a.txt', mode='w', encoding='utf-8') as f1:
    pass
print('__iter__' in dir(f1) and '__next__' in dir(f1))
>>>True
  • Iterable objects can be converted into iterators:
    use the iter() method to convert, and the next() method to sequentially retrieve values.
str1='amwkvi'
obj=iter(str1)      # obj=str1.__iter__() 方法也可以
print(obj)
print(next(obj))    # print(obj.__next__()) 方法也可以
print(next(obj))	# 第二次next取出第二个元素
print(next(obj))	# 一次运行里多次next就会连续往下取值
print(next(obj))    # 但不能超过元素最大数目,否则报错。
>>><str_iterator object at 0x000002848C79CE08>
a
m
w
k
  • Advantages of iterators:
    • Save memory: Every time an element is read, when the next data is read, the previous data will disappear from the memory.
    • Lazy mechanism: next takes one value once and never takes more.
  • Disadvantages of iterators:
    • Slow: trading time for space.
    • You can't go back to get a value, you can only go down in order, and you can't go back to get a value.

3. Comparison between iterable objects and iterators

  • Data reading:
    • Iterable object: When reading data, you can repeatedly retrieve the value without being affected by the last read position.
    • Iterator: When reading data in an object, it will start reading downwards from the last read position and cannot return.
  • Application:
    • Iterable object: It has many private methods, flexible operations (such as addition, deletion, modification and search of lists, dictionaries, common operation methods of strings, etc.), relatively intuitive, and relatively small storage data (millions of objects, 8G memory is affordable) a data set.
      When you focus on flexible processing of data and sufficient memory space, setting the data set as an iterable object is a clear choice.
    • Iterator: It is a very memory-saving data set that can record the value position and directly obtain the value through the loop + next method, but it is not intuitive and the operation method is relatively simple.
      When the amount of your data is too large, large enough to burst your memory, or when saving memory is your first priority, setting the data set as an iterator is a good choice. (Please refer to why python sets the file handle to an iterator).

4. Use while loop to simulate for loop traversing iterable objects

# 用while循环模拟for循环遍历可迭代对象
list1 = ['abc', 11, 66, 22, 'aef', 44, 'internet', 'H3C', 'Open', 88]       # 创建列表,可迭代对象
obj = iter(list1)               # 将可迭代对象转换为迭代器
while 1:                        # while循环开始
    try:                        # 试着运行以下语句
        print(next(obj))        # 用next方法逐个输出包含的元素
    except StopIteration:       # 出现StopIteration错误时
        break                   # 中止循环

Picture sharing

Insert image description here

Guess you like

Origin blog.csdn.net/Jo_Francis/article/details/126028167