# 高级特性
# 1.切片
L = ["1","2","3","4"]
print("L的元素:",L) # ==> 输出 L的元素: ['1', '2', '3', '4']
# 1.1 取出前三个元素
print("L[1]=%s,L[2]=%s,L[3]=%s"%(L[1],L[2],L[3])) # ==> 输出 L[1]=2,L[2]=3,L[3]=4
# 1.2 切片取出: 从索引0开始取,直到索引3为止,但不包括索引3
print("切片取出L[0:3]=",L[0:3]) # ==> 切片取出L[0:3]= ['1', '2', '3']
# 1.3 取出倒数第一个元素
print("L[-1]=%s"%L[-1]) # ==> L[-1]=4
# 1.4 切片取出倒数第一个第二个元素: 倒取元素顺序:-1、-2、-3......
print("倒数第二个元素%s"%L[-2:-1]) # ==> 输出 倒数第二个元素['3'] 从-2开始算起(包含-2)但不包括-1
print("L[-2]:%s"%L[-2])
# 备注: 切片的倒数第一个元素的是从-1开始取的所以要取出倒数第一个元素直接L[-1]
print(L[0:-1])
# ******* 字符串切片 ******** 每隔两个取出对应元素
L1 = "abcdefghijklmnopqrstuvwxyz"
print("L1隔两个元素输出所有:",L1[::2])
print("简写取出前三个元素:",L1[:3])
print("取出所有:",L1[:])
# 2.迭代:for循环遍历称之为迭代
dic = {"a","b","c","d"}
for x in dic :
print("x=%s"%x) # ==> 输出 x=b x=a x=c x=d dic是无序的 所以输出也可能也是无序的
# 2.1 那么,如何判断一个对象是可迭代对象呢?方法是通过collections模块的Iterable类型判断:
print("*****迭代***********")
from collections import Iterable
print(isinstance('abc', Iterable)) # str是否可迭代 ==> 输出 True
print(isinstance([1,2,3], Iterable)) # list是否可迭代 ==> 输出 True
print(isinstance(123, Iterable)) # 整数是否可迭代 ==> 输出 False
# 2.2 可迭代对象,不管是否有下标,都可以迭代
d = {'a': 1, 'b': 2, 'c': 3}
for key in d:
print("dic迭代key",key) # ==> dic迭代key b dic迭代key a dic迭代key c
for value in d.values():
print("dic迭代value",value) # ==> dic迭代value 2 dic迭代value 1 dic迭代value 3
for k,v in d.items():
print("key=%s,value=%s"%(k,v)) # ==> key=b,value=2 key=a,value=1 key=c,value=3
# 2.3 有下标的迭代
for i,value in enumerate(["a","b","c"]):
print(i,value) # ==> 0 a 1 b 2 c
for x,y in [(1,1),(2,2),(4,3)]:
print(x,y) # ==> 1 1 2 2 4 3
# 3.列表生成式
# 3.1 要生成list [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]可以用list(range(1, 11)):
list(range(1, 11)) # ==> 输出[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# 3.2 循环生成list
L2 = [x*x for x in range(1, 11)] # ==> [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
# 3.3 循环生成list并加以判断
[x * x for x in range(1, 11) if x % 2 == 0] # ==> [4, 16, 36, 64, 100]
# 3.4 还可以使用两层循环,可以生成全排列:
[m + n for m in 'ABC' for n in 'XYZ'] # ==> ['AX', 'AY', 'AZ', 'BX', 'BY', 'BZ', 'CX', 'CY', 'CZ']
# 练习 一个list既包含字符串又包含数字 把字符串小写输出
L = ["Hello","World",24,"fengwei"]
L1 = []
for i in L:
if (isinstance(i,str)):
L1.append(i.lower())
else:
L1.append(i)
print("结果:",L1) # ==> 结果: ['hello', 'world', 24, 'fengwei']
# 解法二
L3 = [string.lower() for string in L if isinstance(string,str) == True]
print("L3:",L3) # ==> L3: ['hello', 'world', 'fengwei']
L4 = [string.lower() if isinstance(string,str) else string for string in L]
print("L4:",L4) # ==> L4: ['hello', 'world', 24, 'fengwei']
L5 = [string.upper() if isinstance(string,str) else string for string in L]
print("L5:",L5) # ==> L5: ['HELLO', 'WORLD', 24, 'FENGWEI']
L6 = [s[:1].upper()+s[1:].lower() if isinstance(s,str) else s for s in L]
print("L6:",L6) # ==> L6: ['Hello', 'World', 24, 'Fengwei']
# 4.生成器: 在循环的过程中不断推算出后续的元素,不必创建完整的list,从而节省大量的空间
# 4.1创建方法一:把列表生成的[]改成()就可以创建一个generator(生成器)
gener = (x*x for x in range(10))
print("生成器:",gener) # ==> 生成器: <generator object <genexpr> at 0x10ac0be60>
for g in gener:
print("next输出:",g) # ==> next输出: 0 next输出: 1 next输出: 4 .......
# 备注: 当生成一个生成器我们取出内容时直接用for循环取出 next(gener) 只能一个一个的取出基本无用 并且不需要关心StopIteration的错误
# 斐波拉契数列: 除第一个和第二个数外,任意一个数都可由前两个数相加得
def feiBoList(a):
output = []
if isinstance(a,str) :
print("类型错误")
output.append(1)
else:
p = list(range(a))
p.pop(0)
p.insert(1,1)
for i,x in enumerate(p):
if i == 0 or i == 1:
output.append(x)
else:
m = output[i-2]
n = output[i-1]
output.append(m+n)
if (m+n) > a:
break
return output
print("斐波拉契:",feiBoList(100)) # == > 斐波拉契: [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144]
# 方法2
# Fibonacci series: 斐波纳契数列
# 两个元素的总和确定了下一个数
a, b = 0, 1
while b < 10:
print(b)
a, b = b, a+b
# 5. 如果一个函数定义中包含yield关键字,那么这个函数就不再是一个普通函数,而是一个generator
# 备注: 理解的就是generator和函数的执行流程不一样。函数是顺序执行,遇到return语句或者最后一行函数语句就返回。而变成generator的函数,在每次调用next()的时候执行,遇到yield语句返回,再次执行时从上次返回的yield语句处继续执行
# 练习 输出杨辉三角
def yangHuiFunc():
L = [1]
while True:
yield L
L = [1] + [ L[x-1] + L[x] for x in range(1,len(L)) ] + [1]
# 解释: 调用test函数
# n = 0 ,yield L 直接打印L[1] ==> [1]
# n = 1 L[1] range(1,1)不会执行 : L = [1] + [L[0] + L[1] for x in range(1,1)] + [1] == > [1,1]
# n = 2 L[1,1] range(1,2) = [1] : L = [1] + [L[0] + L[1] for x in range(1,2)] + [1] == > [1,2,1]
# n = 3 L[1,2,1] range(1,3) = [1,2] : L1 = [L[0] + L[1] for x in range(1,3)] == > [3]
# L2 = [L[1] + L[2] for x in range(1,3)] == > [3]
# L = [1] + L1 + L2 + [1] == > [1,3,3,1]
# n = 4 L[1,3,3,1] range(1,4) = [1,2,3] : L1 = [L[0] + L[1] for x in range(1,3)] == > [4]
# L2 = [L[1] + L[2] for x in range(1,3)] == > [6]
# L3 = [L[2] + L[3] for x in range(1,4)] == > [4]
# L = [1] + L1 + L2 + L3 + [1] == > [1,4,6,4,1]
# n = ......
n = 0
for x,t in enumerate(yangHuiFunc()):
# print("第%d个"%x)
print(t)
n = n + 1
if n == 10:
break
#[1]
#[1, 1]
#[1, 2, 1]
#[1, 3, 3, 1]
#[1, 4, 6, 4, 1]
#[1, 5, 10, 10, 5, 1]
#[1, 6, 15, 20, 15, 6, 1]
#[1, 7, 21, 35, 35, 21, 7, 1]
#[1, 8, 28, 56, 70, 56, 28, 8, 1]
#[1, 9, 36, 84, 126, 126, 84, 36, 9, 1]