#!/usr/bin/env python # -*- coding:utf-8 -*- # Author:woshinidaye #列表、元组、字符串、字典、集合、文件、字符编码转码、内置函数 #列表、元组、字符串、字典、集合、文件、字符编码转码、内置函数 #列表、元组、字符串、字典、集合、文件、字符编码转码、内置函数 ''' #三元运算 a,b,c = 1,3,6 d = a if a > b else c print(d) ''' ''' test = '我测试一下' print(test.encode('utf-8')) test1 = b'\xe6\x88\x91\xe6\xb5\x8b\xe8\xaf\x95\xe4\xb8\x80\xe4\xb8\x8b' print(test1.decode('utf-8')) ''' ''' bbbb = b'example' ssss = '学习' ssss_to_bbbb = bytes(ssss,encoding='utf-8') ssss_to_bbbb_2 = str.encode(ssss) ssss_to_bbbb_3 = ssss.encode('utf-8') print(ssss_to_bbbb) print(ssss_to_bbbb_2) print(ssss_to_bbbb_3) bbbb_to_ssss = str(bbbb,encoding='utf-8') print(bbbb_to_ssss,type(bbbb_to_ssss),type(bbbb)) bbbb_to_ssss_2 = bytes.decode(bbbb) print(bbbb_to_ssss_2,type(bbbb_to_ssss_2),type(bbbb)) bbbb_to_ssss_3 = bbbb.decode('utf-8') print(bbbb_to_ssss_3,type(bbbb_to_ssss_3),type(bbbb)) ''' #列表!!!! #列表!!!! #列表!!!! ''' students = ['stu1','stu2','stu3','stu4','stu5'] #print(students,type(students)) #print(students[1],'\n',students[-1],'\n',students[1:3],'\n',type(students[-1]),'\n',students[-3]) #切片 #print(students[-2:],'\n',students[-2:-1]) students.append('stu6') #列表最后插入,增 students.insert(1,'stu7') #插入,增 print(students) #students.remove('stu7') #删 #del students[1] #删 #students.pop() #Remove and return item at index (default last) #students.pop(1) print(students) print('=====>',students.index('stu1')) ''' ''' for i in range(0,len(students)): #print('====>序号:%s,名称:%s' % (i+1,students[i])) #print('====>>>序号:{0:.2f},名称:{1}'.format(i,students[i])) print(f'====>>>>>序号:{i:.2f},名称:{students[i]}') ''' ''' #列表的改! students[students.index('stu1')] = 'stu1111' print(students) students.append('stu1') students.append('stu1') students.sort() print(students,students.count('stu1')) students.reverse() print(students,students.count('stu1')) ''' ''' students_01 = ['stu1','stu2','stu3','stu4','stu5'] students_02 = ['qwe','qwe','wer','ert','rty'] students_01.extend(students_02) students_01.sort() print(students_01,students_02) ''' ''' #Return a shallow copy of the list. students_01 = ['stu1','stu2','stu3','stu4','stu5'] students_02 = students_01.copy() print('====>',students_01,'\n',students_02) students_01[0] = 'stu1111111' print('====>',students_01,'\n',students_02) #替换students_01元素,已经复制的列表不会受影响; ''' ''' students_01 = ['stu1','stu2','stu3','stu4','stu5'] students_01[0] = ['test01','test02'] #print('====>>>>>',students_01[0],'\n',students_01[0][0]) students_02 = students_01.copy() #这个地方的copy似乎只copy了第一层!!!! print('====>>>>>',students_02) students_01[0][0] = 'test1111111' #print(students_01[0],'\n',students_01[0][0]) print('====>>>>>',students_01,'\n',students_02) #子列表里面的元素也发生了修改!!!!! ''' #如果想要实现深copy,就需要使用copy模块 ''' import copy students_01 = ['stu1','stu2','stu3','stu4','stu5'] students_01[0] = ['test01','test02'] #students_02 = copy.copy(students_01) #这样copy和上面的方法类似!!没啥区别 students_02 = copy.deepcopy(students_01) print('---1---',students_01,'\n',students_02) students_01[0][0] = 'test1111111' print('---2---',students_01,'\n',students_02) #这样copy的话,copy之后,子列表的元素被修改,students_02也不会被修改! ''' #浅copy的另外实现方式 ''' import copy students_01 = ['stu1','stu2','stu3','stu4',[100,200,300]] print(students_01) students_02 = copy.copy(students_01) students_03 = students_01[:] students_04 = list(students_01) print(students_02,'\n',students_03,'\n',students_04) #浅copy的用户,创建联合账号,关联账号 information_family = ['成员',['存款',10000]] family_member_1 = information_family[:] family_member_2 = copy.copy(information_family) family_member_1[1][1] = 10000-5000 print(family_member_1) print(family_member_2) #成员看到的信息是一致的 ''' ''' b = 1 a = b b = 2 print('a=',a,'b=',b) #a=1,b=2 c = [1,2,3] d = c c[1] = 4567 print(c,d) #a=[1, 4567, 3],b=[1, 4567, 3] 有点差别哟 ''' ''' students_01 = ['stu1','stu2','stu3','stu4','stu5'] print(students_01[::2]) print(students_01[:-1:2]) print(students_01[0:-1:2]) for i in students_01: print(i) ''' #元组!!!tuple tuple #元组!!!tuple tuple #元组!!!元组就是只读行列表!!!一旦创建,不能修改 ''' students = ('stu1','stu2','stu3','stu4','stu5') print(students.count('stu1'),students.count('stu3')) print(students.index('stu1')) ''' students = ['stu1','stu2','stu3','stu4','stu5'] students.insert(1,[1,2]) print(students)