python内置函数

iter(object)

生成迭代器

  • object - 支持迭代的集合对象
1
2
3
4
5
6
7
8
l = [1, 2, 3]
for i in iter(l):
print(i)
'''
1
2
3
'''

next(iterable)

返回迭代器的下一个值,一般和iter()一起使用

  • iterable - 可迭代对象
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 首先获得Iterator对象:
it = iter([1, 2, 3])
# 循环:
while True:
try:
# 获得下一个值:
x = next(it)
print(x)
except StopIteration:
# 遇到StopIteration就退出循环
break
'''
1
2
3
'''

any(iterable)

any判断给定的可迭代参数iterable是否全部为False,如果有一个为True,就返回True
返回:如果全为空、0、false返回false,否则返回true

  • iterable - 元祖或列表
1
2
3
4
any([1,2,3])
'''
True
'''
Error: API rate limit exceeded for 34.230.114.103. (But here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.)