Python学习笔记2_一些小程序
2024-09-07 01:43:38
counts = [98,12,3,4,1,4,9,3821]
minNum = min(counts)
#print minNum
minNum_index = counts.index(minNum)
#print minNum_index #找出列表中最小的2个元素
def find_two_smallest(L):
smallest = min(L)
min_index = L.index(smallest)
L.remove(smallest) smallest2 = min(L)
min_index2 = L.index(smallest2)
L.remove(smallest2) L.insert(min_index,smallest)
L.insert(min_index2,smallest2) if min_index <= min_index2:
min_index2 +=1 return (smallest,smallest2) ansList = find_two_smallest(counts)
print ansList #第二种方式
def find_two_smallest2(L):
tempList = L[:]
tempList.sort()
return (tempList[0],tempList[1]) ansList2 = find_two_smallest2(counts)
print ansList2 #搜索
def linear_search(v,L):
i = 0
while i<len(L) and L[i] != v:
i = i+1
return i print linear_search(7,counts)
print linear_search(12,counts) #计算花费的时间
import time
def linear_search2(v,L):
i = 0
for value in L:
if value == v:
return i
i = i+1
return len(L) L = range(1000001)
time1 = time.time()
linear_search(7,L)
linear_search(500000,L)
time2 = time.time()
print (time2-time1)*1000 L = range(10)
print L #二分搜索
def binary_search(v,L):
i = 0
j = len(L) -1
while i <= j:
m = (i+j)/2
if(L[m]<v):
i = m +1
else:
j = m - 1
if 0 <= i <len(L) and L[i] == v:
return i
else:
return -1 #测试
VALUES = [1,3,4,6,8,9,10]
assert binary_search(1,VALUES) == 0
assert binary_search(2,VALUES) == -1
最新文章
- maven阿里云中央仓库
- jquer 事件,选择器,dom操作
- 跟我学习dubbo-Dubbo管理控制台的安装(3)
- 10位顶级PHP大师的开发原则
- 从对偶问题到KKT条件
- Tomcat 7 Connector 精读(2) CoyoteAdapter
- http://poj.org/problem?id=2253
- 常见sql的error解决方法
- git 查看文件修改记录
- ADO.NET DataReader和DataAdapter的区别
- java的访问控制(包、访问修饰符、修饰符)
- ubuntu12.04 安装和配置jdk1.7
- ubuntu安装nVidia驱动,遇到终端闪砾问题并解决
- 框架学习:ibatis框架的结构和分析
- Python【第四篇】函数、内置函数、递归、装饰器、生成器和迭代器
- hashtable and hashmap
- oracle undo表空间
- Codeforces Round #373 (Div. 2) C. Efim and Strange Grade 水题
- SSO单点登录之Asp.Net实现示例
- 解决安装Egit时Egit Mylyn和org.eclipse.team.core报错
热门文章
- 【线段树分治 01背包】loj#6515. 「雅礼集训 2018 Day10」贪玩蓝月
- 【数学 技巧】divisor
- nodejs开发过程中遇到的一些插件记录
- spring事务(Transaction )报 marked as rollback-only异常的原因及解决方法
- atag信息处理
- 可持久化treap(FHQ treap)
- BZOJ 5064: B-number
- 【Codeforces Round #476 (Div. 2) [Thanks, Telegram!] C】Greedy Arkady
- .NET开发时让人头痛的SESSION超时
- Django中间件、Auth认证