常用迭代函数
1、 map(func,iter1)
def f1(x):
return x**2
n =[5,1,3,4,5,6]
z = map(f1,n)
print(type(z))
'''返回map 值'''
for i in z:
print(i)
2、zip(iter,iter2)
n =[5,1,3,4,5,6]
m =[8,6,9,87]
y = zip(n,m)
print(type(y))
for j in y:
print(j)
返回结果:
<class 'zip'>
(5, 8)
(1, 6)
(3, 9)
(4, 87)
生成字典:
for i in z:
print(i)
y = zip(n,m)
print(type(y))
for j in y:
print(j)
d={}
for k, v in zip(n,m): '''不能用y代替'''
d.update({k:v}) '''生成一个字典'''
print(d)
返回值:
(5, 8)
(1, 6)
(3, 9)
(4, 87)
{5: 8, 1: 6, 3: 9, 4: 87}
3、enumerate(sequence,[start=0]): 枚举函数
for h in enumerate(n):
print(h)
返回值:元组tuple
(0, 5)
(1, 1)
(2, 3)
(3, 4)
(4, 5)
(5, 6)
4、range 生成整数 range(start, end , step)
for a in range(-9,10,2):
print(a)
返回值:
-9
-7
-5
-3
-1
1
3
5
7
9
5、 arange() 生成小数
import numpy as ap
添加numpy库
print( ap.arange(60,62,0.25))
for i in ap.arange(60,65,0.5):
print(i)
返回值: 数组array
[60. 60.25 60.5 60.75 61. 61.25 61.5 61.75]
60.0
60.5
61.0
61.5
62.0
62.5
63.0
63.5
64.0
64.5