# 高級特性
# 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 切片取出倒數第一個第二個元素: 倒取元素順序:-一、-二、-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]