join() 方法
用于将序列中的元素以指定的字符连接生成一个新的字符串,序列可以使 列表,也可以是 元祖。并且内部元素必须是以字符串的形式存储的。
#!/usr/bin/python
# -*- coding: UTF-8 -*-
str = "-"
seq = ("a", "b", "c") # 字符串序列
print(str.join(seq))
输出结果为(字符串):a-b-c
li = ['I', 'love', 'China', '.']
''.join(li)
输出结果为(字符串): IloveChina.
numpy.tile(A, B) 方法
-- 把A重复B次
>>> import numpy
>>> numpy.tile([0,0],5)#在列方向上重复[0,0]5次,默认行1次
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
>>> numpy.tile([0,0],(1,1))#在列方向上重复[0,0]1次,行1次
array([[0, 0]])
>>> numpy.tile([0,0],(2,1))#在列方向上重复[0,0]1次,行2次
array([[0, 0],
[0, 0]])
>>> numpy.tile([0,0],(3,1))
array([[0, 0],
[0, 0],
[0, 0]])
>>> numpy.tile([0,0],(1,3))#在列方向上重复[0,0]3次,行1次
array([[0, 0, 0, 0, 0, 0]])
>>> numpy.tile([0,0],(2,3))#在列方向上重复[0,0]3次,行2次
array([[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0]])
numpy.sum(矩阵,axis=0) 或者 矩阵.sum(axis=0)
>>> import numpy as np
>>> a=np.sum([[0,1,2],[2,1,3]]) # 全部元素相加
>>> a
9
>>> a.shape # a为一具体数值, type(a)为int类型
()
>>> a=np.sum([[0,1,2],[2,1,3]],axis=0) # 按列相加
>>> a
array([2, 2, 5])
>>> a.shape
(3,)
>>> a=np.sum([[0,1,2],[2,1,3]],axis=1) # 按行相加
>>> a
array([3, 6])
>>> a.shape
(2,)
argsort()函数是将x中的元素从小到大排列,提取其对应的index(索引号)
One dimensional array:一维数组
>>> x = np.array([3, 1, 2])
>>> np.argsort(x)
array([1, 2, 0])
Two-dimensional array:二维数组
>>> x = np.array([[0, 3], [2, 2]])
>>> x
array([[0, 3],
[2, 2]])
>>> np.argsort(x, axis=0) #按列排序
array([[0, 1],
[1, 0]])
>>> np.argsort(x, axis=1) #按行排序
array([[0, 1],
[0, 1]])
>>> x = np.array([3, 1, 2])
>>> np.argsort(x) #按升序排列
array([1, 2, 0])
>>> np.argsort(-x) #按降序排列
array([0, 2, 1])
字典 dict.get的用法
通常我在在用字典时,都是按照字典的键值来获取字典里面的元素,举个栗子:
>>> a = {'a':1,'b':2}
>>> a['a']
1
>>> a['c']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'c'
可以看到,如果在取值的时候遇到所输入的键值不存在的情况,程序就会报错,出于某种目的,为了使程序能够继续运行下去,我们就可以使用 a.get(‘a’) 和 a.get(‘c’, ‘not exist’),其中第二个参数就是在key不存在的情况下所返回的值。
a = {'a':1,'b':2}
a.get('c', '不存在')
# 返回 '不存在'
Python中 dict.items() dict.iteritems()区别
Python 文档解释:
- dict.items(): Return a copy of the dictionary’s list of (key, value) pairs.
- dict.iteritems(): Return an iterator over the dictionary’s (key, value) pairs.
dict.items()返回的是一个完整的列表,而dict.iteritems()返回的是一个生成器(迭代器)。
dict.items()返回列表list的所有列表项,形如这样的二元组list:[(key,value),(key,value),...],dict.iteritems()是generator, yield 2-tuple。相对来说,前者需要花费更多内存空间和时间,但访问某一项的时间较快(KEY)。后者花费很少的空间,通过next()不断取下一个值,但是将花费稍微多的时间来生成下一item。
Python 3.x 里面,iteritems() 和 viewitems() 这两个方法都已经废除了,而 items() 得到的结果是和 2.x 里面 viewitems() 一致的。在3.x 里 用 items()替换iteritems() ,可以用于 for 来循环遍历。
sorted() 函数
sort 与 sorted 区别:
sort 是应用在 list 上的方法,sorted 可以对所有可迭代的对象进行排序操作。
list 的 sort 方法返回的是对已经存在的列表进行操作,而内建函数 sorted 方法返回的是一个新的 list,而不是在原来的基础上进行的操作。
语法:
sorted(iterable[, cmp[, key[, reverse]]])
参数说明:
- iterable -- 可迭代对象。
- cmp -- 比较的函数,这个具有两个参数,参数的值都是从可迭代对象中取出,此函数必须遵守的规则为,大于则返回1,小于则返回-1,等于则返回0。
- key -- 主要是用来进行比较的元素,只有一个参数,具体的函数的参数就是取自于可迭代对象中,指定可迭代对象中的一个元素来进行排序。
- reverse -- 排序规则,reverse = True 降序 , reverse = False 升序(默认)。
>>>a = [5,7,6,3,4,1,2]
>>> b = sorted(a) # 保留原列表
>>> a
[5, 7, 6, 3, 4, 1, 2]
>>> b
[1, 2, 3, 4, 5, 6, 7]
>>> L=[('b',2),('a',1),('c',3),('d',4)]
>>> sorted(L, cmp=lambda x,y:cmp(x[1],y[1])) # 利用cmp函数
[('a', 1), ('b', 2), ('c', 3), ('d', 4)]
>>> sorted(L, key=lambda x:x[1]) # 利用key
[('a', 1), ('b', 2), ('c', 3), ('d', 4)]
>>> students = [('john', 'A', 15), ('jane', 'B', 12), ('dave', 'B', 10)]
>>> sorted(students, key=lambda s: s[2]) # 按年龄排序
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
>>> sorted(students, key=lambda s: s[2], reverse=True) # 按降序
[('john', 'A', 15), ('jane', 'B', 12), ('dave', 'B', 10)]
numpy.min(mat) & numpy.min(mat, 0) & numpy.max(mat, 1) 或写成 mat.min(0)
返回矩阵的(按行、列)的最小值(or 最大值)
mat = np.mat([[1,2,3], [4,5,6]])
print(np.min(mat)) # 返回 1
print(np.min(mat,0)) # 返回 [[1 2 3]], 列最小值
print(np.min(mat,1))
# 返回 [[1]
# [4]] 此为行最小值
# 最大值同理
os.listdir(file_add)
返回file_add目录下的文件名组成的list
例如:F:\machinelearninginaction-master\Ch02\digits\trainingDigits 文件夹下包含有1934个 .txt文件
import os
numFile = os.listdir('F:\\machinelearninginaction-master\\Ch02\\digits\\trainingDigits')
print(type(numFile)) # <class 'list'>
print(len(numFile)) # 1934
将list中的string转换为int
a = ['1', '2', '2']
results = map(int, results) # python 2
results = list(map(int, results)) # python 3
# 返回 [1, 2, 3]
# 或者
results = [ int(i) for i in a ]
# 或者
data = ['1','3.2','2']
data = map(eval, data)
print data
# 返回 [1, 3.2, 2]
持续更新.....