Python
条评论python中的一些语法&函数。
原数+1:
x += 1
字符串与数字的转换:
str(2) = '2'
int('2') = 2
- 字符串的提取:
str.strip([chars])
- 用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列
str.split(str="", num=string.count(str))
- 通过指定分隔符对字符串进行切片,如果参数 num 有指定值,则分隔 num+1 个子字符串
- 字符串的拼接:
- 用%拼接:
str = 'There are %s, %s, %s on the table.' % (fruit1,fruit2,fruit3)
str = 'There are %(fruit1)s,%(fruit2)s,%(fruit3)s on the table' % {'fruit1':fruit1,'fruit2':fruit2,'fruit3':fruit3}
%s
str,%d
int,%f
float,%.2f
两位小数float,%8s
8位占位符,%-8s
左对齐
用join()拼接:
1
2temp = ['There are ',fruit1,',',fruit2,',',fruit3,' on the table']
''.join(temp)用format()拼接:
1
2str = 'There are {2}, {1}, {0} on the table'
str.format(fruit1,fruit2,fruit3) #fruit1出现在0的位置1
2str = 'There are {fruit1}, {fruit2}, {fruit3} on the table'
str.format(fruit1=fruit1,fruit2=fruit2,fruit3=fruit3)
遍历list:
1
2
3
4
5
6
7
8
9
10
11
12from collections import Counter, OrderedDict
x = Counter([1,2,2,2,2,3,3,3,4,4,4,4])
# Counter({2: 4, 4: 4, 3: 3, 1: 1})
sorted(x.items())
x.most_common()
x.most_common(1)
sorted(x, key=x.get, reverse=True)
sorted(x.items(), key=lambda pair: pair[1], reverse=True)
y = OrderedDict(x.most_common())numpy.random.uniform(low,high,size): 参考
low: 采样下界,float类型,默认值为0;
high: 采样上界,float类型,默认值为1;
size: 输出样本数目,为int或元组(tuple)类型,例如,size=(m,n,k), 则输出mnk个样本,缺省时输出1个值。
返回值:ndarray类型,其形状和参数size中描述一致。OrderedDict(): 参考
很多人认为python中的字典是无序的,因为它是按照hash来存储的,但是python中有个模块collections(英文,收集、集合),里面自带了一个子类OrderedDict,实现了对字典对象中元素的排序。zip():参考
zip() 函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的列表。
如果各个迭代器的元素个数不一致,则返回列表长度与最短的对象相同,利用 * 号操作符,可以将元组解压为列表。1
2
3
4
5
6
7
8
9a = [1,2,3]
b = [4,5,6]
c = [4,5,6,7,8]
zipped = zip(a,b) # 打包为元组的列表
# list(zipped) : [(1, 4), (2, 5), (3, 6)]
zip(a,c) # 元素个数与最短的列表一致
# [(1, 4), (2, 5), (3, 6)]
zip(*zipped) # 与 zip 相反,*zipped 可理解为解压,返回二维矩阵式
# [(1, 2, 3), (4, 5, 6)]删除list中的元素
remove: 删除单个元素,删除首个符合条件的元素,按值删除
举例说明:1
2
3
4str=[1,2,3,4,5,2,6]
str.remove(2)
str
# [1, 3, 4, 5, 2, 6]pop: 删除单个或多个元素,按位删除(根据索引删除)
1
2
3
4
5
6
7
8
9
10str=[0,1,2,3,4,5,6]
str.pop(1) #pop删除时会返回被删除的元素
# 1
str
# [0, 2, 3, 4, 5, 6]
str2=['abc','bcd','dce']
str2.pop(2)
# 'dce'
str2
# ['abc', 'bcd']del:它是根据索引(元素所在位置)来删除
1
2
3
4
5
6
7
8str=[1,2,3,4,5,2,6]
del str[1]
str
# [1, 3, 4, 5, 2, 6]
str2=['abc','bcd','dce']
del str2[1]
str2
# ['abc', 'dce']
除此之外,del还可以删除指定范围内的值。1
2
3
4str=[0,1,2,3,4,5,6]
del str[2:4] #删除从第2个元素开始,到第4个为止的元素(但是不包括尾部元素)
str
# [0, 1, 4, 5, 6]
del 也可以删除整个数据对象(列表、集合等)1
2
3str=[0,1,2,3,4,5,6]
del str
str #删除后,找不到对象
LabelEncoder
LabelEncoder是用来对分类型特征值进行编码,即对不连续的数值或文本进行编码。其中包含以下常用方法:fit(y)
:fit可看做一本空字典,y可看作要塞到字典中的词。fit_transform(y)
:相当于先进行fit再进行transform,即把y塞到字典中去以后再进行transform得到索引值。inverse_transform(y)
:根据索引值y获得原始数据。transform(y)
:将y转变成索引值。1
2
3
4
5
6
7
8
9>>> le = preprocessing.LabelEncoder()
>>> le.fit(["paris", "paris", "tokyo", "amsterdam"])
LabelEncoder()
>>> list(le.classes_)
['amsterdam', 'paris', 'tokyo']
>>> le.transform(["tokyo", "tokyo", "paris"])
array([2, 2, 1]...)
>>> list(le.inverse_transform([2, 2, 1]))
['tokyo', 'tokyo', 'paris']
product
笛卡尔积 (有放回抽样排列)permutations
排列 (不放回抽样排列)combinations
组合,没有重复 (不放回抽样组合)combinations_with_replacement
组合,有重复 (有放回抽样组合)eg:
1
2import itertools
itertools.product('ABCD', repeat = 2)combinations和permutations返回的是对象地址,iterators(迭代器),可以用list()转换为list
删除内存
del 可以删除多个变量,del a,b,c,d
办法:
import gc (garbage collector)
del a
gc.collect()
马上内存就释放了。enumerate()
函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中。1
2
3
4
5>>>seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1)) # 下标从 1 开始
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]将lst = [ [1, 2, 3], [2, 1, 3], [8, 4, 3] ],变为[1, 2, 3, 2, 1, 3, 8, 4, 3]
myList = [x for j in lst for x in j]
1
2from itertools import chain
mylist = list(chain(*lst))
namedtuple
namedtuple创建一个和tuple类似的对象,而且对象拥有可以访问的属性。这对象更像带有数据属性的类,不过数据属性是只读的。
eg:TPoint = namedtuple('TPoint', ['x', 'y'])
创建一个TPoint类型,而且带有属性x, y.isinstance
用来判断一个对象是否是一个已知的类型,类似 type()。isinstance()
与type()
区别:
type()
不会认为子类是一种父类类型,不考虑继承关系。isinstance()
会认为子类是一种父类类型,考虑继承关系。
使用:isinstance(object, classinfo)
lambda
,map
,filter
,reduce
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35# lambda
square = lambda number: number*number
# map
def inefficientSquare(number):
result = number*number
return result
map(inefficientSquare,my_list)
# map, filter, reduce
number = [1,2,3,4,5,6]
odd_numbers = []
squared_odd_numbers = []
total = 0
# filter for odd numbers:
for number in numbers:
if number%2 == 1:
odd_numbers.append(number)
odd_numbers = filter(lambda n:n%2==1,numbers)
# square all odd numbers:
for number in odd_numbers:
squared_odd_numbers.append(number*number)
squared_odd_numbers = map(lambda n:n*n,odd_numbers)
# calculate total
for number in squared_odd_numbers:
total += number
total = reduce(lambda acc, n: acc+n, suqared_odd_numbers)
pd.applymap(lambda x: x.replace('\'', ''))加载文件
1
2
3for file in os.listdir('/fold'):
filename = os.path.join(domain, file)
data = np.load(filename)
opencv
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
brightness_4
# Python program to read image using OpenCV
# importing OpenCV(cv2) module
import cv2
# Save image in set directory
# Read RGB image
img = cv2.imread('g4g.png')
# Output img with window name as 'image'
cv2.imshow('image', img)
# Maintain output window utill
# user presses a key
cv2.waitKey(0)
# Destroying present windows on screen
cv2.destroyAllWindows()matplot
1
2
3
4
5
6
7
8
9
10
11
12# Python program to read
# image using matplotlib
# importing matplotlib modules
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
# Read Images
img = mpimg.imread('g4g.png')
# Output Images
plt.imshow(img)pil
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17# Python program to read
# image using PIL module
# importing PIL
from PIL import Image
# Read image
img = Image.open('g4g.png')
# Output Images
img.show()
# prints format of image
print(img.format)
# prints mode of image
print(img.mode)加载多张图片
1
2
3
4
5
6
7
8
9plt.figure(figsize=(50,50))
for i in range(len(files)):
face = files[i]
filename = './Faces128/eface_%s.png' %face
img = mpimg.imread(filename)
plt.subplot(10,1,i+1)
plt.imshow(img)
plt.title(ranks[i])保存
numpy.save(file, arr, allow_pickle=True, fix_imports=True)
张量扩充
np.tile(a,[5,1])
矩阵上三角
at = np.triu(am, 0)
按顺序set
1
2
3
4
5
6def ordered_set(x):
y = list(set(x))
y.sort(key=x.index)
return y
hadm_icd9_3s = list(map(ordered_set,hadm_icd9_3['ICD9_3'].values))排序
1
2
3
4
5
6
7
8
9
10
11
12
13
14# 反转
x.reverse()
# 排序
x.sort()
x.sort(reverse=True) # 逆序
# 排序不覆盖源列表
b = sorted(x)
b = sorted(x, reverse=True)
# 返回索引
np.argsort(x)
np.flip(np.argsort(x))读取文件夹下多个文件
1
2
3
4for file in os.listdir('./filefold'):
filename = os.path.join('./filefold', file)
# print(filename)
data = pd.read_csv(filename, header=None)读取与存储
.mat
文件1
2
3
4
5
6
7
8
9
10import scipy.io as scio
# 读取
dataFile = 'data.mat'
data = scio.loadmat(dataFile)
type(data) #查看,data为字典格式
# 存储
scio.savemat(dataFile, {'data':data})