1.深淺拷貝 拷貝模塊 不可變類型(元組除外)拷貝後記憶體地址相同 可變類型,拷貝後會新生成一個記憶體地址 淺拷貝 只拷貝整個數據類型的錶面記憶體地址 無數據類型嵌套 有數據類型嵌套 深拷貝 不管數據類型有幾層都重新創建記憶體地址存儲 特殊性 元組 如果元組只有一層那麼深淺拷貝記憶體地址都不變 如果元組中嵌套 ...
1.深淺拷貝
-
拷貝模塊
import copy
-
不可變類型(元組除外)拷貝後記憶體地址相同
-
可變類型,拷貝後會新生成一個記憶體地址
-
淺拷貝
-
只拷貝整個數據類型的錶面記憶體地址
- 無數據類型嵌套
#不可變類型 v1 = 'sssdddw' v2 = copy.copy(v1) #引用模塊 print(id(v1),id(v2)) #2032429695216 2032429695216 #可變類型,再開闢一塊新記憶體地址進行存儲 v1 = [11,2,3] v2 = copy.copy(v1) print(id(v1),id(v2)) #1782467694976 1782467643392
- 有數據類型嵌套
#第一層可變類型數據的記憶體地址被重新創建,裡層的記憶體地址則沒變 v1 = [1,2,3,[1,2,3]] v2 = copy.copy(v1) print(id(v1),id(v2)) #2510241515712 2510241705472 print(id(v1[3]),id(v2[3])) #2510241567296 2510241567296
-
-
深拷貝
-
不管數據類型有幾層都重新創建記憶體地址存儲
import copy v1 = [1,2,3,[1,2,3]] v2 = copy.deepcopy(v1) print(id(v1),id(v2)) #1391916533888 1391916723584 print(id(v1[3]),id(v2[3])) #1391916585408 1391916722944
-
-
特殊性
-
元組
-
如果元組只有一層那麼深淺拷貝記憶體地址都不變
import copy v1 = (1,2,3) v2 = copy.copy(v1) print(id(v1),id(v2)) #1993940503296 1993940503296 import copy v1 = (1,2,3) v2 = copy.deepcopy(v1) print(id(v1),id(v2)) #2576028699392 2576028699392
-
如果元組中嵌套列表在深拷貝中則會重新開闢記憶體地址
import copy v1 = (1,2,3,[11,22,33]) v2 = copy.deepcopy(v1) print(id(v1),id(v2))
-
-
-
示例
#1 import copy v1 = '123' v2 = copy.copy(v1) print(v1 == v2) #True print(v1 is v2) #True v3 = copy.deepcopy(v1) print(v1 == v3) #True print(v1 is v3) #True #2 import copy v1 = [11,'222',222] v2 = copy.copy(v1) print(v1 == v2) #True print(v1 is v2) #False v3 = copy.deepcopy(v1) print(v1 == v3) #True print(v1 is v3) #False #3 import copy v1 = [11,'222',222,[33,44,22]] v2 = copy.copy(v1) print(v1[3] == v2[3]) #True print(v1[3] is v2[3]) #True v3 = copy.deepcopy(v1) print(v1[3] == v3[3]) #True print(v1[3] is v3[3]) #False
2.文件操作
-
打開文件
file = open('22.txt',mode='r',encoding='utf-8') open(文件路徑,打開方式,文件編碼)
-
讀取和寫入文件
-
r ,read,可讀
-
只能讀不能寫
file = open('22.txt',mode='r',encoding='utf-8') data = file.read() print(data) file.close()
-
-
w, write,可寫
-
打開文件的同時清空文件內容,再寫入數據
-
如果文件不存在則先創建再寫入數據
-
只能寫字元串
file = open('22222.txt',mode='w',encoding='utf-8') file.write('123') file.close()
-
-
a,append 追加
-
追加數據到文件內部數據末尾
file = open('22222.txt',mode='a',encoding='utf-8') file.write('123') file.close()
-
-
r+,可讀可寫
- 可以通過seek調游標位置來指定寫入和查看位置
#文件先寫在查看(清除原有內容) file = open('22222.txt',mode='r+',encoding='utf-8') file.write('123') data = file.read() print(data) #123 file.close() #指定位置寫入(則會覆蓋指定位置字元後面的數據) file = open('22222.txt',mode='r+',encoding='utf-8') file.seek(2) file.write('666') data = file.read() print(data) #這樣只能讀取寫入後游標存在位置之後的數據 file.close() file = open('22222.txt',mode='r+',encoding='utf-8') file.seek(2) file.write('666') file.seek(0) data = file.read() print(data) #這樣可以讀取全部數據 file.close() #如果想從文件末尾去添加數據 file = open('22222.txt',mode='a+',encoding='utf-8') date = file.read() file.write('好') file.seek(0) date = file.read() print(date) #123456好 file.close()
-
w+,可寫可讀
- 打開文件的同時清空文件內容,再寫入數據
- 可以通過seek調游標位來讀取
file = open('22222.txt',mode='w+',encoding='utf-8') file.write('好') file.seek(0) date = file.read() print(date) file.close()
-
a+,追加讀
- 追加數據到文件內部數據末尾
- 可以通過seek調游標位置來讀取數據
file = open('22222.txt',mode='a+',encoding='utf-8') file.write('好') file.seek(0) date = file.read() print(date) #1dfdsfdf好 file.close()
-
-
關閉文件
- 文件操作完畢,文件未關閉情況下文件內部數據會存到記憶體中,並未保存到硬碟
file.close()
-
練習
#練習一、換行寫入數據 file = open('22222.txt',mode='w',encoding='utf-8') while True: read_input = input('請輸入你要說的話(輸入n結束):') if read_input.lower() == 'n': break file.write(read_input + '\n') file.close() file = open('22222.txt',mode='r',encoding='utf-8') date = file.read() print(date) file.close()
#練習二、把下麵列表的內容輸入到文件中保存 ''' user = [ {'name':'alex','pwd':'123'}, {'name':'eric','pwd':'oldboy'} ] ''' user = [ {'name':'alex','pwd':'123'}, {'name':'eric','pwd':'oldboy'} ] file = open('22222.txt',mode='w',encoding='utf-8') for i in user: line = "%s|%s\n"%(i['name'],i['pwd']) file.write(line) file.close() file = open('22222.txt',mode='r',encoding='utf-8') date = file.read() print(date) file.close()
#練習三、把練習二產生的文件裡面的數據,提取到列表中保存 #解法一(適用於數據量少的情況下使用) file =open('22222.txt',mode='r',encoding='utf-8') date = file.read() date = date.strip() date = date.split('\n') print(date) file.close() #解法二 v1 = [] file =open('22222.txt',mode='r',encoding='utf-8') for line in file: line = line.strip() v1.append(line) print(v1) file.close()
#練習四、編寫代碼實現賬號密碼存儲及登入驗證 #創建密碼保存到文件中 file = open('賬號密碼.txt',mode='a',encoding='utf-8') while True: user = input('請輸入用戶名:') if user.lower() == 'n': break pwd = input('請輸入密碼:') file.write('%s|%s\n'%(user,pwd)) file.close() #從文件中驗證賬號密碼 user_pwd = [] file = open('賬號密碼.txt',mode='r',encoding='utf-8') for i in file: i = i.strip() user_pwd.append(i) file.close() state = 1 user_1 = input('請輸入登入用戶名:') pwd_1 =input('請輸入登入密碼:') for item in user_pwd: item_1 = item.split('|') if user_1 == item_1[0] and pwd_1 == item_1[1]: break else: state = 0 break if state: print('登入成功!!!') else: print('登入失敗')