python基础学习第五天

li=[1,2,33,-1,'dbssd',[4,5,6],{4:'rfw',5:'re'}]
del(li[1])
print(li)
print(type(li))
#访问元素
print(li[0])
print(li[-2])
print(li[-1])
#查找元素的位置
i=0
for teli in li:
if teli == 33:
print('元素中找到了%s' %i)
i+=1
print(li.index(33))
#从数组中找到两个数等于目标数的值
print('-------------------------------')
def two_sum(numbers,target):
#range 表示从0开始
for i in range(len(numbers)-1):
for j in range(i+1,len(numbers)):
if numbers[i] + numbers[j] == target:
return i,j
return -1,-1
#默认返回tuple
print(two_sum([2,44,3,21,17,8],20)) #字典 dict key-value 无序的
print('-------------dict------------------')
te_di={1:2,'ed':54,'8e':'tr',7:[8,1,10,0]}
print(type(teli))
print(te_di)
print(te_di['ed'])
#判断key是否存在 在dict内部已经对key做了索引 时间复杂度需要学习?
print(7 in te_di)
print(8 in te_di)
#遍历 级联元素
for key,value in te_di.items():
print(key,value)
ddi=te_di.keys()
print(type(ddi))
print(ddi)
#set 元素没有重复的集合  没有下标的概念
sa=set([1,2,3,4,5,5,60,11,0,32])
sb=set([43,22,32])
print(sa)
#并集
print(sa|sb)
print(sa.union(sb))
#交集
print(sa&sb)
print(sa.intersection(sb))
#差集 sa - sa&sb
print(sa-sb)
print(sa.difference(sb))
print(sb.difference(sa))
#对称差 sa|sb - sa&sb
print(sa.symmetric_difference(sb))
#修改元素
print('---------------')
sb.add(600)
print(sb)
sb.update([44,55,6,77])
print(sb)
sb.remove(600)
print(sb)
#切片生成一个新的对象
print('------切片---------')
qp=list(range(10))
print(qp[::-1])
print(qp[4:8]) #快速推导
print('------快速推导---------')
kstd=[]
for i in range(10):
if i%3 == 0:
kstd.append(i)
print(kstd)
kstd=[1]*10
print(kstd)
kstd=[i*3 for i in range(30)]
print(kstd)
#浅拷贝 一维是赋值操作
kstd=[[0]*3]*3
print(kstd)
kstd[0][0]=100
print(kstd)
#深拷贝
kstd=[[0]*3 for i in range(3)]
print(kstd)
kstd[0][0]=100
print(kstd)
kstd={x for x in range(10) if x%2==0}
print(type(kstd))
print(kstd)
kstd={x: x%2==0 for x in range(10) if x%2==0}
print(type(kstd))
print(kstd)
#生成器是把真正的运算到使用的过程中
print('------生成器---------')
square_t=[]
for i in range(1000):
square_t.append(i*i)
for i in range(10):
print(square_t[i])
square_t=(x*x for x in range(1000))
print(square_t)
for i in range(10):
print(next(square_t))
def fib(limit):
n,a,b = 0,0,1
while n<limit:
yield b
a,b = b,a+b
n+=1
return 'done'
import traceback
f=fib(5)
print(type(f))
print(next(f))
print(next(f))
print(next(f))
print(next(f))
try:
print(next(f))
except StopAsyncIteration:
traceback.print_exc()
#迭代器
from collections import Iterable
from collections import Iterator print('------迭代器---------')
print(isinstance([1,2,3],Iterable))
print(isinstance({},Iterable))
print('------对象---------')
class Student:
def __init__(self,name,age):
self.__name=name
self.__age=age
def detail(self):
print(self.__name)
print(self.__age)
stdu=Student('张三',20)
stdu.name='李四'
stdu.detail()
#继承
class Car:
def __init__(self,wheel,circle):
self.wheel=wheel
self.circle=circle
def detail(self):
print(self.wheel)
print(self.circle)
class BaoMa(Car):
def moreMoney(self):
print('很贵的车')
bao=BaoMa('四个*','一个方向盘')
bao.detail()
bao.moreMoney() #多继承 广度优先
class F1:
pass
class F2(F1):
def show(self):
print('XXX 执行')
class F3:
def show(self):
print('F3 F3 F3')
def func(obj):
obj.show()
print(type(F2))
ff2=F2()
ff3=F3()
func(ff2)
func(ff3)
dir('AFC') 训练算法
上一篇:[LeetCode145]Binary Tree Postorder Traversal


下一篇:2017.3.31 spring mvc教程(二)核心流程及配置详解