- 博客(14)
- 收藏
- 关注
原创 python用生成器生成裴波那契数列
#_*_coding:utf-8_*_ def f(): a,b=1,1 while True: yield a a,b = b,a+b a = f() for i in range(10): print(a.__next__(),end = ' ') 1 1 2 3 5 8 13 21 34 55
2018-09-24 22:05:24
1181
原创 python自定义包与调用
比如我写的包在D:\python27下: 第一种情况,同一目录下,D:\python27\bb,bb为包名,下面有a.py,和b.py,两个可以通过import a 或 import b调用。 a.py 中的方法为 def aa(x): return(x) b.py可以这样调用: import a a.aa(4) 第二种情况,不在同一目录下加入上面的a.p...
2018-09-20 23:07:29
734
原创 numpy
NumPy及向量化 In [100]: 测试在jupyter中进行: import numpy as np 1. 1 创建Array my_list = [1, 2, 3] x = np.array(my_list) print('Array: ', x) Array: [1 2 3] x.shape (3,) x.ndim 1 np.array([1, 2, 3...
2018-09-16 23:03:36
210
原创 Python高阶应用
1、条件表达式 # -*- coding:utf-8 -*- import math # 定义一个函数输入大于零取log,否则返回nan def get_log(x): if x > 0: #技巧在jupyter中查看python内置函数方法:shift + tab y = math.log(x,10) #Docstring: #l...
2018-09-16 21:11:56
244
原创 算法学习_python-9
堆排序: # -*- coding:utf-8 -*- # 堆排序适用于记录数很多的情况 from collections import deque # 这里需要说明元素的存储必须要从1开始 # 涉及到左右节点的定位,和堆排序开始调整节点的定位 # 在下标0处插入0,它不参与排序 #L = deque([1,3,2]) def element_exchange(numbers,low,hig...
2018-09-12 21:16:36
155
原创 算法学习_python-8
随机算法 雇佣者问题: #! /usr/bin/ python #coding=utf-8 #定义一个类,参数有雇佣者名字和得分 class Assitant: def __init__(self,a_name,value): self.name = a_name self.score = value def on_line(assList): ...
2018-09-11 12:36:29
131
原创 算法学习_python-7
最大子序列: #! /usr/bin/env python #coding=utf-8 def find_crossing_subarray(A,low,mid,high): left_sum = float("-inf") sum = 0 max_left = 0 max_right = 0 for i in range(mid,low-1,-1)...
2018-09-10 20:49:01
150
原创 算法学习_python-6
冒泡排序: #_*_coding: utf-8 _*_: def bubbleSort(list1): for j in range(0,len(list1)-1): #增加一个计数器 count = 0 for i in range(len(list1)-1-j,j,-1): if list1[i] < l...
2018-09-08 17:21:03
162
原创 算法学习_python-5
归并排序: #_*_coding: utf-8 _*_: import random def Mergesort(str): if len(str) <= 1: #子序列 return str mid = (len(str) // 2) #递归的切片操作 left = Mergesort(str[:mid]) right = Merge...
2018-09-08 15:47:55
155
原创 算法学习_python-4
选择排序: #_*_coding: utf-8 _*_: def selectsort(list): for i in range(0,len(list)): for j in range(len(list)-1,i,-1): #选择一个最小的 if list[j] < list[i]: ...
2018-09-08 11:13:46
129
原创 算法学习_python-3
插入排序: #_*_coding: utf-8 _*_: def insert(*r): a = list(r) #print(a) for j in range(1,len(a)): key = a[j] i = j while i >0 and a[i-1]>key: a[i] =...
2018-09-07 20:23:39
136
原创 算法学习_python-1
算法复杂度计算: 计算裴波那切算法的复杂度: 代码举例: #_*_coding: utf-8 _*_: def fib(n) : f1 = f2 = 1 #循环n-1次 for k in range(1, n) : #f2 = f2 + f1 #f1 = f2 f1, f2 = f2, f2 + f1 ...
2018-09-07 12:51:34
243
原创 算法学习_python-2
求一个数的平方根: 1、给定的数值必须是非负实数 2、计算是在有穷步内完成。一般而言,计算只能得到非负实数的近似值 代码: # _*_ coding:utf-8 _*_ import math def sqrt(x): #定义y的初始值为1 y = 1.0 #允许的最小误差10e-10 while abs(y*y - x) > 1e-10: ...
2018-09-07 10:17:42
130
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人
RSS订阅