python之迭代

Iterable是迭代在各种语言中常见,在API中的解释

在python中主要通过for  in 实现

迭代对象用isinstance(str,Iterable)去判断则要植入collections集合库

最后一个小问题,如果要对list实现类似Java那样的下标循环怎么办?Python内置的enumerate函数可以把一个list变成索引-元素对,这样就可以在for循环中同时迭代索引和元素本身:

for i,value in enumerate("abcd"):

  print(i,value)

猜你喜欢

转载自www.cnblogs.com/12star52/p/9825536.html