- 列表
从索引0开始,添加元素 name.append(' 要添加的新值')
>>> list=[1,2,3] >>> print(list) [1, 2, 3] >>> print(list[1]) 2 >>> list.append('4') >>> print(list) [1, 2, 3, '4'] >>> print(len(list)) 4
列表语义表示法:list=[x**2 for x in range(1,5) if x%2!=0] print list #输出结果 [1, 9]
列表切片选取:list [start : end : stride]
l = [i ** 2 for i in range(1, 11)] # Should be [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] print l[2:9:2] #输出 [9, 25, 49, 81]
当strides为负时,列表从后往前读:#摘抄一例子 to_five = ['A', 'B', 'C', 'D', 'E'] print to_five[3:] # prints ['D', 'E'] print to_five[:2] # prints ['A', 'B'] print to_five[::2] # print ['A', 'C', 'E']
my_list = range(1, 11) # Add your code below! backwards=my_list[::-2] print backwards #输出[10, 8, 6, 4, 2]
- 字典
采用键值对的形式>>> dic={'name':'tom','age':2} >>> print(dic) {'name': 'tom', 'age': 2} >>> dic['name'] 'tom'
添加新的键值对
内部函数:>>> dic['type']='dog' >>> print(dic) {'name': 'tom', 'age': 2, 'type': 'dog'}
print dict.items() #打印字典所有项,包括key和values print movies.keys() #打印所有的Key print movies.values() #打印所有的values #示例 movies = { "Monty Python and the Holy Grail": "Great", "Monty Python's Life of Brian": "Good", "Monty Python's Meaning of Life": "Okay" } print movies.items() print movies.keys() print movies.values() #结果: [("Monty Python's Life of Brian", 'Good'), ("Monty Python's Meaning of Life", 'Okay'), ('Monty Python and the Holy Grail', 'Great')] ["Monty Python's Life of Brian", "Monty Python's Meaning of Life", 'Monty Python and the Holy Grail'] ['Good', 'Okay', 'Great']
- 循环
for循环:>>> for i in range(len(list)): print(i,list[i]) 0 1 1 2 2 3 3 4
while循环>>> for i in list: print(i,"type:",type(i)) 1 type: <class 'int'> 2 type: <class 'int'> 3 type: <class 'int'> 4 type: <class 'str'>
或者用break跳出循环>>> while(i<len(list)): print(list[i]) i+=1 1 2 3 4
>>> i=0 >>> while(True): print (list[i]) i+=1 if i==2:break 1 2
- 函数
>>> def add(x,y): return x+y >>> print(add(2,3)) 5
lambda
不用使用def定义函数,不需要给函数取名,就可以当成参数传递,相当于“匿名”函数;my_list = range(16) print filter(lambda x: x % 3 == 0, my_list) #输出 [0, 3, 6, 9, 12, 15]
#语义列表和lambda结合使用例子 squares=[x**2 for x in range(1,11)] print filter(lambda x:x>=30 and x<=70,squares) #输出 [36, 49, 64]
- 类
类中第一个函数位__init__(self,),__init__的第一个参数用来指向被创建的类的实例,后面的参数用来指明,当实例化该类的时候需要输入的参数
创建一个类computer,其有一个功能函数实现加法
创建该类一个实例,并且计算加法>>> class computer: def __init__(self,x,y): self.x=x self.y=y def add(self): return self.x+self.y >>> cla1=computer(2,3) >>> print(cla1.add()) 5
>>> class computer: def add(self,x,y): self.x=x self.y=y return self.x+self.y >>> cla2=computer() >>> cla2.add(2,3) 5
类中所有方法第一个参数都是自身的一个实例?,输入参数都是第二个开始
python中的使用习惯,习惯定义类的第一个函数位__init__()用来初始化类的参数变量,同时所有子函数(类中方法)第一个参数为self,用来指向创建的对象
类的派生
调用父类的方法参考:这里class ave(computer): """添加一个子类ave,继承自computer,计算均值""" def __init__(self,x,y): print("1:",computer.add(self,x,y)) #调用父类的方法一 print("2:",super(ave,self).add(x,y)) #调用父类的方法二 def ave_result(self): return (self.x+self.y)/2 >>> cla3=ave(2,6) 1: 8 2: 8 print(cla3.ave_result()) 4.0
类中全局变量和私有变量
is_alive是Animal中的公有变量,所以实例化的所有对象都可以调用该变量
但是name和age是实例化的对象私有的变量,只有实例化的对象可以调用,其他对象不可以调用class Animal(object): """Makes cute animals.""" is_alive = True #声明类中共有变量is_alive def __init__(self, name, age): self.name = name self.age = age zebra = Animal("Jeffrey", 2) giraffe = Animal("Bruce", 1) panda = Animal("Chad", 7) print zebra.name, zebra.age, zebra.is_alive print giraffe.name, giraffe.age, giraffe.is_alive print panda.name, panda.age, panda.is_alive #输出 effrey 2 True Bruce 1 True Chad 7 True
子类重写父类函数后,调用父类原来函数的方法:
super(Derived, self).m()class Employee(object): """Models real-life employees!""" def __init__(self, employee_name): self.employee_name = employee_name def calculate_wage(self, hours): self.hours = hours return hours * 20.00 # Add your code below! class PartTimeEmployee(Employee): def calculate_wage(self, hours): self.hours = hours return hours * 12.00 def full_time_wage(self, hours): return super(PartTimeEmployee, self).calculate_wage(hours) milton = PartTimeEmployee('Milton') print milton.full_time_wage(10) #程序输出200,而不是120
类的继承和调用:
class Point3D(object): def __init__(self,x,y,z): self.x=x self.y=y self.z=z def __repr__(self): print "(%d,%d,%d)" %(self.x,self.y,self.z) my_point=Point3D(1,2,3) my_point.__repr__() #输出(1,2,3)
-
科学计算库numpy
import numpy as np a=np.array([1,2,3]) #一维数组 a[0] #访问一维数组某个元素 b=np.array([[1,2,3],[4,5,6],[7,8,9]]) #二维数组 b[1,2] #访问二维数组具体元素 b[1,:] #访问二维数组一行元素 x=np.array([[1,2],[3,4]]) y=np.array([[5,6],[7,8]]) #矩阵运算 print(x+y) print(x*y) #对应位置元素运算 print(np.dot(x,y)) #矩阵运算 #输出: [[6 8] [10 12]] [[5 12] [21 32]] [[19 22] [43 50]]
-
按位操作
(。。。。,csdn用的不够熟练,小标题怎么变两行了!!!!!!!!!)
与1取或,该位置1
与1取异或,该位反转
右移 >> 左移 << 按位与 & 按位或 | 按位异或 ^ 正整形加1变负 ~
进制转换
bin()十进制转二进制
oct()十进制转八进制
hex()十进制转十六进制
其他进制转十进制:
int ('11',2) 二进制转十进制,输出3
int('11',8) 八进制转十进制,输出9
int('11',16) 十六进制转十进制,输出17
8.python 切割字符串,并去除标点
text_list=re.split(r'\W+',text)
re.splt具体使用方法解释参考:
https://blog.youkuaiyun.com/wl_ss/article/details/78241782
或者:
import re
# Remove punctuation characters
text = re.sub(r"[^a-zA-Z0-9]", " ", text)
print(text)