python基础函数笔记

1、astype与dtype的区别:

astype是转换数据类型,dtype是查看数据类型

In [11]: arr = np.array([1,2,3,4,5])

In [12]: arr
Out[12]: array([1, 2, 3, 4, 5])

// 该命令查看数据类型
In [13]: arr.dtype
Out[13]: dtype('int64')

// 该命令是转换数据类型
In [14]: float_arr = arr.astype(np.float64)

// 该命令查看数据类型
In [15]: float_arr.dtype
Out[15]: dtype('float64')

2、enumerate()函数,使用方法为:

enumerate(X, start=0)
  • 函数中的参数X可以是一个迭代器(iterator)或者是一个序列,start是起始计数值,默认从0开始。 

       X可以是一个字典,也可以是一个序列。

>>> a = {1: 1, 2: 2, 3: 3}
>>> for i , item in enumerate(a):
...     print i, item
Ouput:
0 1
1 2
2 3
>>> b = [1,2,3,4,5,6]
>>> for i , item in enumerate(b):
...     print i, item
Ouput:
0 1
1 2
2 3
3 4
4 5
5 6
>>> for i , item in enumerate(b, start=10):
...     print i, item
Ouput:
10 1
11 2
12 3
13 4
14 5
15 6

enumerate用法总结博客中学习到,enumerate还可以用来统计文件行数,能够处理较大的文件。

count = 0
file_count = open(filepath,'r')
for index, line in enumerate(file_count): 
    count += 1
print count

猜你喜欢

转载自blog.csdn.net/Charlotte_android/article/details/81512586