id(對象) ----> 查看內存地址python
type(對象) ----> 查看對象的類app
bytes(s, encoding='utf-8') ----> 將字符串s轉換成字節, 默認16進制python2.7
str(x, encoding='utf-8') ----> 將字節碼x轉換成字符串編碼
bin() ----> 10進制轉爲2進制spa
UTF-8 編碼, 1 個漢字 3 個字節 ,1 個字節 8 位對象
GBK 編碼, 1 個漢字 2 個字節blog
python3.5 中按字符來處理,好比在for循環中,len("請問")=2索引
python2.7 中按字節來處理,len("請問")=6 (utf8)內存
list(): 能夠將字符串,元組,字典dic.values() dic.items() dic 轉爲列表utf-8
s = '你好' l = list(s) #for循環s的每個元素做爲列表中的元素 print(l) #結果爲: ['你', '好']
dic = { 'k1': 'popo', 'k2': 'qwqw', 'k3': 'qweqwe' } li = list(dic) print(li) #結果爲 ['k3', 'k2', 'k1']
dic = { 'k1': 'popo', 'k2': 'qwqw', 'k3': 'qweqwe' } li = list(dic.items()) print(li) #結果爲 [('k3', 'qweqwe'), ('k1', 'popo'), ('k2', 'qwqw')]
dic = { 'k1': 'popo', 'k2': 'qwqw', 'k3': 'qweqwe' } li = list(dic.values()) print(li) #resoult: ['qweqwe', 'qwqw', 'popo']
列表 索引 和 切片 的區別, 可見索引取出單個元素,而切片取出的是集合(跟原類型一致)
>>> li = ["abc", "cde", "fgh"] >>> li[1] 'cde' >>> li[1:2] ['cde']
能夠跟其它類型相互嵌套
>>> tup = ('abc', 'cde', ['aaa', 'bbb', 'ccc'], {'k1': 'v1', 'k2': 'v2'}) >>> print(tup) ('abc', 'cde', ['aaa', 'bbb', 'ccc'], {'k1': 'v1', 'k2': 'v2'}) >>> tup[2].insert(1, 'xxx') >>> print(tup) ('abc', 'cde', ['aaa', 'xxx', 'bbb', 'ccc'], {'k1': 'v1', 'k2': 'v2'})
在字典中追加鍵值對
tup = ('abc', 'cde', ('aaa', 'bbb', 'ccc', {'k1': 'v1', 'k2': 'v2'})) print(tup) tup[2][3].update({'k3': 'v3'}) print(tup) tup[2][3]['k4'] = 'v4' print(tup) #>>>>>>> ('abc', 'cde', ('aaa', 'bbb', 'ccc', {'k2': 'v2', 'k1': 'v1'})) ('abc', 'cde', ('aaa', 'bbb', 'ccc', {'k2': 'v2', 'k3': 'v3', 'k1': 'v1'})) ('abc', 'cde', ('aaa', 'bbb', 'ccc', {'k4': 'v4', 'k2': 'v2', 'k3': 'v3', 'k1': 'v1'}))
建立字典:
d = dict(k1 = 'v1', k2 = 'v2')
print(d)
#>>>>>>
{'k2': 'v2', 'k1': 'v1'}
l = ['qqq', 'www', 'eee']
di = dict(enumerate(l, 8))
print(di)
#>>>>>>
{8: 'qqq', 9: 'www', 10: 'eee'}
fromkeys() 的特別之處
dic = dict.fromkeys(['k1', 'k2', 'k3'], []) print(dic) dic['k3'].append('xxx') print(dic) #>>>>>> {'k3': [], 'k2': [], 'k1': []} {'k3': ['xxx'], 'k2': ['xxx'], 'k1': ['xxx']} d2 = {'k1': [], 'k2': [], 'k3': []} print(d2) d2['k3'].append('xxx') print(d2) #>>>>>> {'k3': [], 'k2': [], 'k1': []} {'k3': ['xxx'], 'k2': [], 'k1': []}