Chapter VII python basis of supplementary data types (base e)
7.1 Supplementary Data Types
String str, int int, list list, tuple tuple, dictionary dict.
7.1.1 String str type of supplement
s1 = str(123) #类型字符串
s.capitalize () first letter capitalized
s = "alex wusir"
s1 = s.capitalize() #首字母大写:Alex wusir
print(s1)
s.title () capitalize the first letter of each word
s = "alex wusir"
s1 = s.title() #每个单词前面首写字母大写:Alex Wusir
print(s1)
The number of statistics appear s.count ()
s = "alex awusair"
print(s.count("a")) #3次
s.find () Find string
s = "alex taibai"
print(s.find("e")) #dind 查找出对应的索引值
print(s.find("c")) # find 查找不到的时候返回 -1
print(s.index("C")) # index 查找不到就报错
Find all the index a (subscript):
s = "alex taibai"
for i in range(len(s)): #range范围是s字符串的长度
if s[i] == 'a':
print(i)
7.1.2 list list type to add:
li = list("815678234") # 定义的方式
print(li)
['8', '1', '5', '6', '7', '8', '2', '3', '4']
count () statistics
li = list("815678234")
print(li.count("1")) #统计1出现1次
index () View
print(li.index("4")) #查看"4"的索引值为8
print(li)
li.reverse () anyway
li.reverse() #['4', '3', '2', '8', '7', '6', '5', '1', '8']
print(li)
li.sort () to sort
li.sort() # 升序:['1', '2', '3', '4', '5', '6', '7', '8', '8']
li.sort(reverse=True) # 降序:['8', '8', '7', '6', '5', '4', '3', '2', '1']
print(li)
7.1.3 tuple type of supplement tuplel
tu = tuple("12345")
print(tu)
('1', '2', '3', '4', '5')
count () statistics
tu = ('1', '2', '3', '4', '5')
print(tu.count("3")) #统计"3"出现的次数
index () Find
print(tu.index("4")) #4对应的索引值为3
3
7.1.4 Dictionary dict type of supplement
dic = dict(k=1,k1=123,k2="aa",k3=222,k4=1123123) # 定义方式
print(dic)
{'k': 1, 'k1': 123, 'k2': 'aa', 'k3': 222, 'k4': 1123123}
dic.popitem () # remove a random dictionary key pair, delete the key return to form Ganso
dic = {'k': 1, 'k1': 123, 'k2': 'aa', 'k3': 222, 'k4': 1123123}
dic2 = dic.popitem() #随机删除返回删除内容('k4', 1123123)
print(dic)
('k4', 1123123)
{'k': 1, 'k1': 123, 'k2': 'aa', 'k3': 222}
python3.6 version default to delete the last key-value pairs
dict.fromkeys () to create a batch Dictionary
dic = {}
dic1 = dict.fromkeys("abcdef",[4,5,6])
# 第一个参数可迭代对象
# 第二个参数是每个键对应的值 -- 用的都是同一个内存地址
print(dic1)
{'a': [4, 5, 6], 'b': [4, 5, 6], 'c': [4, 5, 6], 'd': [4, 5, 6], 'e': [4, 5, 6], 'f': [4, 5, 6]}
dic1["a"].remove(5)
print(dic1)
{'a': [4, 6], 'b': [4, 6], 'c': [4, 6], 'd': [4, 6], 'e': [4, 6], 'f': [4, 6]}
7.1.5 collection set supplemental type
s = set("1234") # 定义方式
print(s)
{'2', '1', '3', '4'}
7.2 Data type conversion
String str ===> Integer int string must be a decimal number to be converted.
s = "abc"
n = int(s)
print(n,type(n))#报错 n = int(s) ValueError: invalid literal for int() with base 10: 'abc'
s = "123" #必须是十进制的数
n = int(s)
print(n,type(n))
123 <class 'int'>
Integer int ===> string str
n = 123
s = str(n)
print(s,type(s))
String str ==> list list
s = "123"
li = list(s)
print(li)
['1', '2', '3']
s = "123"
print(s.split()) #把字符串转化成列表:['123']
print(''.join(li)) # 把列表转换成字符串:123
List list ==> string str
li = ["12","3"]
s = str(li) #把列转成字符串['12', '3'] <class 'str'>
print(s,type(s))
print(s)
print(''.join(li)) #用join把列转换成字符串 不能有数字,变成123
123
List list ==> tuple tuple
li = [1,2,3]
tu = tuple(li)
print(tu)
Tuple tuple ==> list list
tu = (1,2,3)
li = list(tu)
print(li)
Collection set ==> list list
s = {12,3,4}
print(list(s))
List list ==> collection set
li = [1,2,3]
s = set(li)
print(s)
Data type conversion summary:
String - Digital: string must be a decimal number are
numbers - String: Direct Conversion
List - string:. '' Join () - objects may not appear in the iterative digital
string - List : split
addition to the dictionary, between the container data type conversion may be directly
Order-disorder type data summary:
有序: str,int,bool,list,tuple
无序: dict,set
可变: list,dict,set
不可变: str,int,bool,tuple
Data access methods types:
Direct Access: int, bool, set
sequential access: list, tuple, str
through the key access: dict
7.2 Common Problems
7.2.1 list li inside index value is deleted odd
li = [1,2,3,4,5] # corresponding to the index value: 0,1,2,3,4
Questions:
= [1,2,3,4,5] # [1,3,4,5]
Delete the index value is odd
I in Range for (. 4):
IF I. 1% 2 ==:
li.pop (I) # results not
print (li) # Results [1,3,4] for a loop execution would last 3 to index
To list the index value method is to remove the odd:
1.1 reverse deleted
li = [1,2,3,4,5]
for i in range(len(li)-1,-1,-1):
if i % 2 == 1:
li.pop(i) # 倒序删除
print(li)
1.2 tamper with the concept delete
偷换感念
li = [1,2,3,4,5]
new_li = []#要删的值
for i in range(len(li)):
if i % 2 == 1:
new_li.append(li[i]) #通过索引值取到元素
for em in new_li:
li.remove(em) #拿到计数的索引值要删除的值
print(li)
###方法
li = [1,2,3,4,5]
for i in range(len(li)-1,-1,-1):
if i % 2 == 1:
del li[i] # 倒序删除
print(li)
###方法
li = [1,2,3,4,5]
del li[1::2]
print(li)
使用for删除列表的时候从左向右删除,会报错.结果不对
dic = {"k1":"v1","k2":"v2"}
for i in dic:
dic["k3"] = "v3"
print(dic)
面试题:
li = [1,2,3,4]
for i in li:
li.append(i)
print(li)
Dictionary common errors:
You can not change the size in traversing the dictionary itself to mass delete keys of the dictionary right.
Title: circulating in the dictionary dic add a couple of keys: "k3": v3
dic = {"k1":"v1","k2":"v2","k3":"v3"}
for i in dic:
dic["k7"] = "1"
print(dic) #报错:RuntimeError: dictionary changed size during iteration
Added to the new list by deleting the cycle value.
dic = {"k1":"v1","k2":"v2","k3":"v3"}
li = []
for i in dic:
if i == "k1" or i == "k3":
li.append(i)
for em in li:
dic.pop(em)
print(dic)
7.3 Advanced coding
ascii does not support Chinese
gbk GB Chinese 2 English 1
Unicode Unicode Chinese English 2 4
UTF-8 English 1 Europe 2 Asia 3
7.3.1 decoding, encoding
Chinese turn into bytes, the first encoding, decoding encoded
s = "今天晚上,吃鸡!"
s1 = s.encode("gbk") # 中文转换成字节 -- 编码
s1 = s.encode("utf-8") # 中文转换成字节 -- 编码
b'\xbd\xf1\xcc\xec\xcd\xed\xc9\xcf,\xb3\xd4\xbc\xa6!'
print(s1.decode("utf-8")) #字节转换成字符串 -- 解码
s = "你好"
s1 = s.encode("utf-8") # 字节
print(s1) # 6个字节
print(s1.decode("gbk")) # 3个字
is unicode python3 memory usage
is ascii python2 memory usage