python 坑1

1.编码解码

编码:将文字转换成字节形式 encode

name = '小冯'
print(name.encode('utf-8'))	# b'xe5xb0x8fxe5x86xaf'

解码:将字节转换成文字形式 decode

name = '小冯'
msg = name.encode('utf-8')
print(msg.decode()) # 小冯

2.基础数据类型补充:

2.1 str:

首字母大写

name = 'xiao,feng'
print(name.capitalize()) # Xiao,feng

**每个单词首字母大写 **

print(name.title()) # Xiao,Feng

**大小写反转 **

name = 'xiao,fenG'
print(name.swapcase()) # XIAO,FENg

居中 -- 填充

print(name.center(20,'*')) # *****xiao,feng******

查找:find index

print(name.find('f')) # 返回索引值 5
print(name.find('y')) # 如果没有 则返回 -1

print(name.index('f')) # 返回索引值 5
print(name.index('y')) # 报错 ValueError: substring not found

**拼接 **

print('_'.join(name)) # x_i_a_o_,_f_e_n_g

格式化: name.format

msg = '啦啦啦{},{},{}'
print(msg.format(1,2,3)) # 啦啦啦1,2,3

msg = '啦啦啦{2},{0},{1}'
print(msg.format(1,2,3)) # 啦啦啦3,1,2

msg = '啦啦啦{q},{w},{e}'
print(msg.format(q = 5,e = 9,w = 3)) # 啦啦啦5,3,9

2.2list:

**排序(默认是升序) 降序sort(reverse=True) **

lst = [1,2,3,4,5]
lst.sort(reverse = True)
print(lst) # [5, 4, 3, 2, 1]

lst.sort()
print(lst) # [1, 2, 3, 4, 5]

反转

lst.reverse()
print(lst) # [5, 4, 3, 2, 1]

查找 index

print(lst.index(4)) # 3
print(lst.index(9)) # 报错 ValueError: 9 is not in list

**统计 count **

print(lst.count(5)) # 1

+ - * 元素都是共用的 会开辟一个新的内存空间

面试:

lst = [[]]
new_lst = lst * 5
new_lst[0].append(10)
print(new_lst) # [[10], [10], [10], [10], [10]]
lst = [1,[]]
new_lst = lst * 5
new_lst[0] = 10
print(new_lst) # [10, [], 1, [], 1, [], 1, [], 1, []]
lst = [1,[]]
new_lst = lst * 5
new_lst[1] = 10
print(new_lst) # [1, 10, 1, [], 1, [], 1, [], 1, []]
lst = [1,2,3]
new_lst = lst * 5
print(id(new_lst), id(new_lst[0])) # 1813566569096 1756851216

2.3tuple:

​ **(1,) # 元组 **

print(type((1,))) # <class 'tuple'>

​ **(1) # 括号里数据本身 **

print(type(1)) # <class 'int'>

2.4dict:

popitem 随机删除字典中的键值对

dic = {"key":1,"key2":2,"key3":56}
print(dic.popitem()) # ('key3', 56)
print(dic) # {'key': 1, 'key2': 2}
  **fromkeys("可迭代的键",共用的值)   -- 坑 **
dic = {}
dic.fromkeys("123",[23]) # 批量添加键值对{"1":[23],"2":[23],"3":[23]}
print(dic) # {}

dic = dict.fromkeys("123456789",1) # 批量添加键值对"键是可迭代对象",值 -- 会被共用
dic["1"] = 18 
print(dic) # {'1': 18, '2': 1, '3': 1, '4': 1, '5': 1, '6': 1, '7': 1, '8': 1, '9': 1}

dic(key = 1,key2 =2)

print(dict(key = 1,key2 = 2)) # {'key': 1, 'key2': 2}

2.5set:

​ **set() -- 空集合 **

set("alex") # 迭代添加

3.坑

print(list('qwe'))  # ['q', 'w', 'e']
print(tuple('qwe')) # ('q', 'w', 'e')
print(dict('qwe'))  # ValueError: dictionary update sequence element #0 has length 1; 2 is required
print(set('qwe'))   # {'q', 'e', 'w'}
lst = [1,2]
for i in lst:
    lst.append(3)
print(lst)  # 死循环
lst = [1,2,3,4]
for i in lst:
    lst.pop()
print(lst) # [1, 2]
lst = [1,2,3,4]
for i in lst:
    lst.pop()
print(lst) # [1, 2]
lst = [1,2,3,4]
for i in lst:
    lst.pop(0)
print(lst) # [3, 4]
lst = [1,2,3,4]
for i in lst:
    lst.remove(i)
print(lst) # [2, 4]

列表删除 -- 从后向前删除

lst = [1,2,3,4,5]
for i in range(len(lst)):
    lst.pop()
print(lst)

lst = [1,2,3,4,6]
for i in range(len(lst)-1,-1,-1):
    del lst[i]
print(lst)

lst = [1,2,3,4,6]
for i in range(len(lst)):
    del lst[-1]
print(lst)

创建一个新的列表,删除旧的列表

lst = [1,2,3,4,5,6]
lst1 = lst.copy()
for i in lst1:
    lst.remove(i)
print(lst)

​ **字典删除 -- 循环的时候不能改变源数据的大小 (可以改变值) **

dict.fromkeys('123456',[])

​ **创建一个新的字典,删除旧的字典 **

dic = dict.fromkeys("12345",1)
dic1 = dic.copy()
for i in dic1:
    dic.pop(i)
print(dic)

集合删除 -- 循环的时候不能改变源数据的大小

4.类型转换:

​ **list -- str join **

str -- list split

set - list

list - set

5.数据类型:

可变:list ,dict ,set
不可变:int bool str tuple
有序:list,tuple,str,int,bool
无序:dict,set
取值方式:
索引: str list tuple
直接: set ,int ,bool
**键: dict **

bool: False
数字: 0
字符串: ""
列表:[]
元组:()
字典:{}
集合: set()
其他: None

原文地址:https://www.cnblogs.com/fengqiang626/p/11179808.html