标签:方法 列表、元组、字典 port style 迭代器 print *** bsp ble
‘‘‘ 判断是否可迭代 字符串、列表、元组、字典、集合都可以被for循环,说明他们都是可迭代的 ‘‘‘ from collections.abc import Iterable l = [1, 2, 3, 4] t = (1, 2, 3, 4) d = {1: 2, 3: 4} s = {1, 2, 3, 4} print(isinstance(l, Iterable)) print(isinstance(t, Iterable)) print(isinstance(d, Iterable)) print(isinstance(s, Iterable))
‘‘‘ 可迭代的内部实现了__iter__方法 ‘‘‘ print(‘__iter__‘ in dir([1,2])) print(‘__iter__‘ in dir((2,3))) print(‘__iter__‘ in dir({1:2})) print(‘__iter__‘ in dir({1,2}))
iter_l = [1,2,3,4,5,6].__iter__() #获取迭代器中元素的长度 print(iter_l.__length_hint__()) #根据索引值指定从哪里开始迭代 print(‘*‘,iter_l.__setstate__(4)) #一个一个的取值 print(‘**‘,iter_l.__next__()) print(‘***‘,iter_l.__next__())
标签:方法 列表、元组、字典 port style 迭代器 print *** bsp ble
原文地址:https://www.cnblogs.com/huay/p/10837652.html