python基础数据类型补充以及编码的进阶
Posted Ethan272
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python基础数据类型补充以及编码的进阶相关的知识,希望对你有一定的参考价值。
基础数据类型补充内容
字符串
s = "everyone‘s the hero in their own story" # print(s.capitalize()) # 句子首单词的首字母变大写,其余小写 Everyone‘s the hero in their own story # print(s.swapcase()) # 全部变大写 EVERYONE‘S THE HERO IN THEIR OWN STORY # print(s.title()) # 每个单词的首字母变大写 Everyone‘S The Hero In Their Own Story # print(len(s)) # 38 # print(s.center(39, "*")) # 内同居中,总长度,空白处填充 *everyone‘s the hero in their own story # print(s.center(45, "*")) # ****everyone‘s the hero in their own story*** # print(s.center(48, "*")) # *****everyone‘s the hero in their own story***** # 寻找字符串中的元素是否存在 # print(s.find("hs")) # 通过元素找索引,返回的找到的第一个元素的索引,如果找不到返回-1 -1 # print(s.find("ev")) # 0 # print(s.find("ev", 9, 18)) # -1 # print(s.find("th", 11, 20)) # 11 # print(s.index("hs")) # 通过元素找索引,返回的找到的第一个元素的索引,找不到报错。 报错 # print(s.index("ev")) # 0 # print(s.index("ev", 9, 18)) # 报错 # print(s.index("th", 11, 20)) # 11
元组
python中元组有一个特性,元组中如果只含有一个元素且没有逗号,则该元组不是元组,与该元素的数据类型一致,如果有逗号,那么它是元组。
# tu1 = (1) # print(tu1, type(tu1)) # 1 <class ‘int‘> # tu2 = ("alex") # print(tu2, type(tu2)) # alex <class ‘str‘> # tu3 = ([1, 2, 3]) # print(tu3, type(tu3)) # [1, 2, 3] <class ‘list‘> # tu1 = (1,) # print(tu1, type(tu1)) # (1,) <class ‘tuple‘> # tu2 = ("alex",) # print(tu2, type(tu2)) # (‘alex‘,) <class ‘tuple‘> # tu3 = ([1, 2, 3],) # print(tu3, type(tu3)) # ([1, 2, 3],) <class ‘tuple‘>
index:通过元素找索引(可切片),找到第一个元素就返回,找不到该元素即报错。
tup = (1, 2, "alex", "你好", True, [1, 2, 3,]) # print(tup.index(1)) # 0 # print(tup.index("alex")) # 2 # print(tup.index(True)) # 0 # print(tup.index([1, 2, 3,])) # 5
count: 获取某元素在列表中出现的次数
# print(tup.count(1)) # 2
列表
li = ["b", "a", "r", "r", "y"] # print(li.count("r")) # 2 # print(li.index("a")) # 1 # print(li.index("r")) # 2 # li.sort() # 默认从小到大 li.sort(reverse = True) # 从大到小 # print(li) # [‘a‘, ‘b‘, ‘r‘, ‘r‘, ‘y‘] # li.reverse() # print(li) # [‘y‘, ‘r‘, ‘r‘, ‘a‘, ‘b‘]
列表的相加和相乘整数
l1 = [1, 2, 3] l2 = [2, 4, "tough", 6] # print(l1 + l2) # [1, 2, 3, 2, 4, ‘tough‘, 6] # print(l1 * 3) # [1, 2, 3, 1, 2, 3, 1, 2, 3]
循环列表,改变列表大小的问题
l3 = [11, 22, 33, 44, 55]
需求: 请把索引为奇数对应的元素删除(不能一个一个删除,此l1只是举个例子,里面的元素不定)。
# 正常思路做法 # for index, item in enumerate(l3): # print(index, item) # print(l3[index]) # if index % 2 != 0: # l3.remove(l3[index]) # print(l3) # [11, 33, 44] # for index in range(len(l3)): # if index % 2 == 1: # l3.pop(index) # print(l3) # [11, 33, 44] # method1 直接删除 # del l3[1::2] # print(l3) # [11, 33, 55] # method 2 倒序删除 # print(len(l3)) # 5 # for index in range(len(l3) - 1, -1, -1): # # print(index) # 4 3 2 1 # if index % 2 != 0: # l3.pop(index) # print(l3) # [11, 33, 55] # method 3 思维置换 # new_list = [] # for index, item in enumerate(l3): # if index % 2 == 0: # new_list.append(l3[index]) # print(new_list) # [11, 33, 55] # l3 = new_list # print(l3) # new_list = [] # for index in range(len(l3)): # if index % 2 == 0: # new_list.append(l3[index]) # print(new_list) # [11, 33, 55] # l3 = new_list # print(l3)
总结:循环一个列表时,最好不要改变列表的大小,因为他会影响你的最终结果
dict
fromkeys:创建一个字典:字典的所有键来自一个可迭代对象,字典的值使用同一个值
# dic = dict.fromkeys("abc", 100) # print(dic) # {‘a‘: 100, ‘b‘: 100, ‘c‘: 100} # dic = dict.fromkeys([1, 2, 3], "hello") # print(dic) # {1: ‘hello‘, 2: ‘hello‘, 3: ‘hello‘} # 坑: 值共享一个 # dic = dict.fromkeys([1, 2, 3], []) # print(dic) # {1: [], 2: [], 3: []} # dic[1].append(666) # print(dic) # {1: [666], 2: [666], 3: [666]} # print(id(dic[1]),id(dic[2]),id(dic[3])) # 2905006740296 2905006740296 2905006740296
# popitem 3.5版本之前,popitem为随机删除, 3.6之后为删除最后一个,有返回值 dic = {"name": "李海", "age": 19} # res = dic.popitem() # print(dic, res) # {‘name‘: ‘李海‘} (‘age‘, 19) # update # dic.update(gender = "male", weight = 65) # {‘name‘: ‘李海‘, ‘age‘: 19, ‘gender‘: ‘male‘, ‘weight‘: 65} # print(dic) # dic.update((("height", 180), ("hobby", "football"), ("address", "CDC"))) # print(dic) # {‘name‘: ‘李海‘, ‘age‘: 19, ‘height‘: 180, ‘hobby‘: ‘football‘, ‘address‘: ‘CDC‘} # dic.update([("height", 180), ("hobby", "football"), ("address", "CDC")]) # print(dic) # {‘name‘: ‘李海‘, ‘age‘: 19, ‘height‘: 180, ‘hobby‘: ‘football‘, ‘address‘: ‘CDC‘} # dic1 = {"name":"jin","age":18,"sex":"male"} # dic2 = {"name":"alex","weight":75} # # dic1.update(dic2) # print(dic1) # {‘name‘: ‘alex‘, ‘age‘: 18, ‘sex‘: ‘male‘, ‘weight‘: 75} # print(dic2) # {‘name‘: ‘alex‘, ‘weight‘: 75}
循环字典,改变字典大小的问题
dic3 = {‘k1‘: ‘太白‘, ‘k2‘: ‘barry‘, ‘k3‘: ‘白白‘, ‘age‘: 18}
需求: 请将字典中所有键带k元素的键值对删除
# for i in dic3: # # print(i) # k1 k2 k3 age # if "k" in i: # del dic3[i] # print(dic3) # 报错 RuntimeError: dictionary changed size during iteration # method 1 # li = [] # # for key in dic3.keys(): # for key in dic3: # if "k" in key: # li.append(key) # # print(li) # [‘k1‘, ‘k2‘, ‘k3‘] # for item in li: # # dic3.pop(item) # del dic3[item] # print(dic3) # method 2 for key in list(dic3.keys()): if "k" in key: dic3.pop(key) print(dic3)
总结: 在循环一个字典的过程中,不要改变字典的大小(增,删字典的元素),这样会直接报错。
数据类型间的转换问题
现在学过的数据类型有:int、bool、str、list、tuple、dict、set
int、bool 、str 三者转换
# int ---> bool i = 100 print(bool(i)) # True # 非零即True i1 = 0 print(bool(i1)) # False 零即False # bool ---> int t = True print(int(t)) # 1 True --> 1 t = False print(int(t)) # 0 False --> 0 # int ---> str i1 = 100 print(str(i1)) # ‘100‘ # str ---> int # 全部由数字组成的字符串才可以转化成数字 s1 = ‘90‘ print(int(s1)) # 90 # str ---> bool s1 = ‘太白‘ s2 = ‘‘ print(bool(s1)) # True 非空即True print(bool(s2)) # False # bool ---> str t1 = True print(str(True)) # ‘True‘
str、list 转换
# str ---> list s = "alex is gay" print(s.split()) # [‘alex‘, ‘is‘, ‘gay‘] # list ---> str # 前提 list 里面所有的元素必须是字符串类型才可以 li = [‘alex‘, ‘is‘, ‘gay‘] print(" ".join(li)) # "alex is gay"
list、set 转换
# list ---> set s1 = [1, 2, 3] print(set(s1)) # {1, 2, 3} # set ---> list set1 = {1, 2, 3, 3,} print(list(set1)) # [1, 2, 3]
str、bytes 两者转换
# str ---> bytes s1 = ‘太白‘ print(s1.encode("utf-8")) # b‘xe5xa4xaaxe7x99xbd‘ # bytes ---> str n = b‘xe5xa4xaaxe7x99xbd‘ print(n.decode("utf-8")) # ‘太白‘
所有数据都可以转化成bool值
转化成bool值为False的数据类型有: ‘‘, 0, (), {}, [], set(), None
基础数据类型的总结
按存储空间的占用分(从低到高)
数字 字符串 集合:无序,即无序存索引相关信息 元组:有序,需要存索引相关信息,不可变 列表:有序,需要存索引相关信息,可变,需要处理数据的增删改 字典:有序,需要存key与value映射的相关信息,可变,需要处理数据的增删改(3.6之后有序)
按存值个数区分
标量/原子类型 | 数字,字符串 |
容器类型 | 列表,元组,字典 |
按可变不可变区分
可变 | 列表,字典 |
不可变 | 数字,字符串,元组,布尔值 |
按访问顺序区分
直接访问 | 数字 |
顺序访问(序列类型) | 字符串,列表,元组 |
key值访问(映射类型) | 字典 |
以上是关于python基础数据类型补充以及编码的进阶的主要内容,如果未能解决你的问题,请参考以下文章