規則:java
方法:python
n=2:c++
n=3:程序員
n=n:函數
def hano(n, a, b, c): ''' 漢諾塔的遞歸實現 n:表明幾個塔 a:表明第一個塔,開始的塔 b:表明第二個塔,中間過渡的塔 c:表明第三個塔,目標塔 ''' if n == 1: print(a, "-->", c) return None ''' if n== 2: print(a, "-->", b) print(a, "-->", c) print(b, "-->", c) return None ''' # 把n-1個塔,從a塔藉助於c塔,挪到b塔上去 hano(n-1, a, c, b) print(a, "-->", c) # 把n-1個塔,從b塔,藉助於a塔,挪到c塔上去 hano(n-1, b, a, c) a = 'A' b = 'B' c = 'C' n = 1 hano(n, a, b, c)
A --> C
n = 2 hano(n, a, b, c)
A --> B A --> C B --> C
n = 3 hano(n, a, b, c)
A --> C A --> B C --> B A --> C B --> A B --> C A --> C
n = 5 hano(n, a, b, c)
A --> C A --> B C --> B A --> C B --> A B --> C A --> C A --> B C --> B C --> A B --> A C --> B A --> C A --> B C --> B A --> C B --> A B --> C A --> C B --> A C --> B C --> A B --> A B --> C A --> C A --> B C --> B A --> C B --> A B --> C A --> C
# del 刪除 a = [1,2,3,4,5,6] del a[2] print(a)
[1, 2, 4, 5, 6]
# del 刪除 # 若是使用del以後,id的值和刪除前不同,則說明刪除生成了一個新的List a = [1,2,3,4,5,6] print(id(a)) del a[2] print(id(a)) print(a)
1787345100360 1787345100360 [1, 2, 4, 5, 6]
# del一個變量後不能再繼續使用此變量 del a print(a)
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-15-a09b634934b2> in <module> 1 # del一個變量後不能再繼續使用此變量 ----> 2 del a 3 print(a) NameError: name 'a' is not defined
# 使用加號鏈接兩個列表 a = [1,2,3,4,5] b = [5,6,7,8,9] d = ['a', 'b', 'c'] c = a + b + d print(c)
[1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 'a', 'b', 'c']
# 使用乘號操做列表 # 列表直接跟一個整數想乘 # 至關於把n個列表接在一塊兒 a = [1,2,3,4,5] b = a * 3 print(b)
[1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
# 成員資格運算 # 就是判斷一個元素是否在列表list裏邊 a = [1,2,3,4,5,6] b = 8 # c的值是一個布爾值 c = b in a print(c) b = 4 print(b in a)
False True
# not in a = [1,2,3,4,5] b = 9 print(b not in a)
True
# for in list a = [1,2,3,4,5] # 挨個打印a裏邊的元素 for i in a: print(i)
1 2 3 4 5
# java, c++ 程序員寫的python代碼是這樣的 for i in range(0,len(a)): print(a[i]) i += 1
1 2 3 4 5
b = ["I love you"] for i in b: print(i)
I love you
# range # in 後面的變量要求是能夠迭代的內容 for i in range(1,10): print(i) print(type(range(1,10)))
1 2 3 4 5 6 7 8 9 <class 'range'>
# while循環訪問list # 通常不用while遍歷list a = [1,2,3,4,5,6] length = len(a) # indx表示的是list的下標 indx = 0 while indx < length: print(a[indx]) indx += 1
1 2 3 4 5 6
# 雙層列表循環 # a爲嵌套列表,或者叫雙層列表 a = [["one", 1], ["two", 2], ["three", 3]] for k,v in a: print(k, "--", v)
one -- 1 two -- 2 three -- 3
# 雙層列表循環變異 # a爲嵌套列表,或者叫雙層列表 a = [["one", 1, "enis"], ["two", 2], ["three", 3,4,5,6,8]] for k,v in a: print(k, "--", v)
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-44-db702adbc386> in <module> 4 a = [["one", 1, "enis"], ["two", 2], ["three", 3,4,5,6,8]] 5 ----> 6 for k,v in a: 7 print(k, "--", v) ValueError: too many values to unpack (expected 2)
# 雙層列表循環變異 # a爲嵌套列表,或者叫雙層列表 a = [["one", 1, "enis"], ["two", 2, "zwei"], ["three", 3, "drei"]] # 這個例子說明:k,v,w的個數應該跟解包出來的變量個數一致 for k,v,w in a: print(k, "--", v, "--", w)
one -- 1 -- enis two -- 2 -- zwei three -- 3 -- drei
# for 建立 a = ['a', 'b', 'c'] # 用List a建立一個List b # 下面的代碼的含義是,對於全部a中的元素,逐個放入新列表b中 b = [i for i in a] print(b)
['a', 'b', 'c']
# 對a中全部元素乘以10,生成一個新List a = [1, 2, 3, 4, 5] # 用List a建立一個List b # 下面的代碼的含義是,對於全部a中的元素,逐個放入新列表b中 b = [i*10 for i in a] print(b)
[10, 20, 30, 40, 50]
# 還能夠過濾原來List中的內容並放入新列表 # 好比原有列表,須要把全部a中的偶數生成新的列表b a = [x for x in range(1,35)] # 生成從1到34的一個列表 # 把a中全部偶數生成一個新的列表 b b = [m for m in a if m % 2 == 0] print(b)
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34]
# 列表生成式能夠嵌套 # 有兩個列表a,b a = [i for i in range(1,4)] # 生成List a print(a) b = [i for i in range(100,400) if i % 100 == 0] print(b) # 列表生成是能夠嵌套,此時等於兩個for循環嵌套 c = [m+n for m in a for n in b] print(c) # 上面代碼跟下面代碼等價 for m in a: for n in b: print(m+n, end=" ") print() # 嵌套的列表生成也能夠用條件表達式 c = [m+n for m in a for n in b if m+n < 250] print(c)
[1, 2, 3] [100, 200, 300] [101, 201, 301, 102, 202, 302, 103, 203, 303] 101 201 301 102 202 302 103 203 303 [101, 201, 102, 202, 103, 203]
# len:求列表長度 a = [x for x in range(1,100)] print(len(a)) # max:求列表中的最大值 # min:同理 print(max(a)) b = ["man", "film", "python"] print(max(b))
99 99 python
# List:將其餘格式的數據轉換成List a = [1,2,3] print(list(a))
[1, 2, 3]
s = "I love you" print(list(s))
['I', ' ', 'l', 'o', 'v', 'e', ' ', 'y', 'o', 'u']
# 把range產生的內容轉換成list print(list(range(12,19)))
[12, 13, 14, 15, 16, 17, 18]