python3 str、list、dict的幾種常用方法
阿新 • • 發佈:2019-02-13
str()
str.capitalize()
x = "asd123"
print(x.capitalize()) # 首字母大寫
執行結果:
Asd123
str.center(width [, fill])
str.ljust(width [, fill])
str.rjust(width [, fill])
str.zfill(width)
x = "asd123" print(x.center(10, "*")) # 使str中間化,width代表總長度,fill代表填充物,預設為" " print(x.ljust(10, "*")) # 右向填充fill,總長度width,預設為" " print(x.rjust(10, "*")) # 左向填充fill,總長度width,預設為" " print(x.zfill(10)) # 左向填充"0",總長度width 執行結果: **asd123** asd123**** ****asd123 0000asd123
str.maketrans(x [, y [, z]])
str.translate(map)
x = "asd123ewq321"
print(x.translate(str.maketrans("asd", "123", "qwe"))) # 將"asd"單個字元,替換成"123",刪除字元"qwe"
執行結果:
123123321
str.split(sep [, maxsplit])
str.splitlines([keepends])
str.partition(sep)
str.rpartition(sep)
x = "asd\nqwe\nzxc\n" print(x.split("\n")) # 分割"\n",輸出列表 print(x.split("\n", 1)) # 分割"\n",輸出列表,分割1次 print(x.splitlines()) # 按"\n"、"\r"分割,輸出列表 print(x.splitlines(True)) # 按"\n"、"\r"分割,輸出列表,保留"\n"、"\r" print(x.partition("\n")) # 左向分割"\n",輸出元組 print(x.rpartition("\n")) # 右向分割"\n",輸出元組 執行結果: ['asd', 'qwe', 'zxc', ''] ['asd', 'qwe\nzxc\n'] ['asd', 'qwe', 'zxc'] ['asd\n', 'qwe\n', 'zxc\n'] ('asd', '\n', 'qwe\nzxc\n') ('asd\nqwe\nzxc', '\n', '')
str.swapcase()
str.upper()
str.lower()
x = "AsDQWEzxc"
print(x.swapcase()) # 大小寫轉換
print(x.upper()) # 全部大寫
print(x.lower()) # 全部小寫
執行結果:
aSdqweZXC
ASDQWEZXC
asdqwezxc
list()
list.append() 與 list.extend()的區別:
x = ["a", "b", "c", "d"] y = [1, 2, 3, 4] x.append(y) print(x) #執行結果: ['a', 'b', 'c', 'd', [1, 2, 3, 4]]
x = ["a", "b", "c", "d"]
y = [1, 2, 3, 4]
x.extend(y)
print(x)
執行結果:
['a', 'b', 'c', 'd', 1, 2, 3, 4]
list[i: j] = [X, Y] 進行列表修改:
x = ["a", "b", "c", "d"]
x[3] = 4
print(x)
x[0:3] = [1, 2, 3]
print(x)
執行結果:
['a', 'b', 'c', 4]
[1, 2, 3, 4]
list.sort() 與 sorted(list) 的用法:
x = [2, 1, 4, 3]
y = ["b", "A", "d", "C"]
z = y.copy() # 使用 list.copy() 複製列表
x.sort()
print(x)
y.sort(key=str.upper, reverse=True) # 自動將字串轉換成大寫排列,並反轉,但不修改list中的值
print(y)
z.sort(reverse=True)
print(z)
執行結果:
[1, 2, 3, 4]
['d', 'C', 'b', 'A']
['d', 'b', 'C', 'A']
x = [2, 1, 4, 3]
y = ["b", "A", "d", "C"]
print(sorted(x))
print(x) # sorted(list) 不會影響原list,而是新生成一個list
print(sorted(y))
print(sorted(y, key=str.upper))
print(sorted([i.upper() for i in y], reverse=True))
執行結果:
[1, 2, 3, 4]
[2, 1, 4, 3]
['A', 'C', 'b', 'd']
['A', 'b', 'C', 'd']
['D', 'C', 'B', 'A']
list.reverse() 與 reversed(list) 的用法:
x = [1, 2, 3, 4]
y = ["a", "b", "c", "d"]
x.reverse()
print(x)
print(list(reversed(y))) # reversed() 產生的使一個迭代
print(y)
執行結果:
[4, 3, 2, 1]
['d', 'c', 'b', 'a']
['a', 'b', 'c', 'd']
list.pop()、list.remove() 與 del list 的用法
x = ["a", "b", "c", "d"]
y = x.copy() # 使用 list.copy() 複製列表
z = x[:] # 使用 list[:] 複製列表
print(x.pop(1)) # 刪除並返回刪除的元素
print(x)
print(y.remove("b")) # 刪除指定元素,但不會返回刪除元素
print(y)
del z[1]
print(z)
執行結果:
b
['a', 'c', 'd']
None
['a', 'c', 'd']
['a', 'c', 'd']
list.insert()、list.count() 等用法:
x = ["a", "b", "c", "d", "d", "c", "a", "c"]
print(x.count("c"))
print(x)
x.insert(1, "c")
print(x)
print(x.count("c"))
執行結果:
3
['a', 'b', 'c', 'd', 'd', 'c', 'a', 'c']
['a', 'c', 'b', 'c', 'd', 'd', 'c', 'a', 'c']
4
dict()
構造字典的幾種方法:
x = {}
x[1] = "a"
x[2] = "b"
x[3] = "c"
print(x)
y = {1: "a", 2: "b", 3: "c"}
print(y)
z = dict.fromkeys([1, 2, 3], 0) # 該方法只能創造 key 值,所有的 value = 0,預設為 None
print(z)
zz = dict(a=1, b=2, c=3) # 該方法只能用變數作為 key 值
print(zz)
a = [1, 2, 3]
b = ["a", "b", "c"]
print(dict(zip(a, b))) # zip 構造法
執行結果:
{1: 'a', 2: 'b', 3: 'c'}
{1: 'a', 2: 'b', 3: 'c'}
{1: 0, 2: 0, 3: 0}
{'a': 1, 'b': 2, 'c': 3}
{1: 'a', 2: 'b', 3: 'c'}
列印與判斷字典中 key、value 的方法:
x = {1: "a", 2: "b", 3: "c"}
if 1 in x: # key 值判定
print("1 在字典 x 中!")
else:
print("1 不在字典 x 中!")
print(x.keys()) # 輸出全部 key 值
print(x.values()) # 輸出全部 value 值
print(x.items()) # 輸出 key + value
執行結果:
1 在字典 x 中!
dict_keys([1, 2, 3])
dict_values(['a', 'b', 'c'])
dict_items([(1, 'a'), (2, 'b'), (3, 'c')])
根據字典中的 key 值輸出 value 的兩種方法:
x = {1: "a", 2: "b", 3: "c"}
print(x[1])
print(x.get(1))
執行結果:
a
a
dict.update() 以及根據 key 值進行簡單的排序:
x = {1: "a", 4: "d", 5: "f"}
y = {2: "b", 3: "c", 5: "e"}
print(x)
x.update(y) # 將 y 合入並更新 x
print(x)
z = zip(x.keys(), x.values())
print(min(zip(x.keys(), x.values()))) # 輸出 x 中最小值
print(max(zip(x.keys(), x.values()))) # 輸出 x 中最大值
print(dict(sorted(zip(x.keys(), x.values())))) # 根據 key 值進行排序
執行結果:
{1: 'a', 4: 'd', 5: 'f'}
{1: 'a', 4: 'd', 5: 'e', 2: 'b', 3: 'c'}
(1, 'a')
(5, 'e')
{1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e'}
dict.pop()與 del dict[key] 的方法:
x = {1: "a", 2: "b", 3: "c", 4: "d"}
y = x.copy()
print(x.pop(2)) # 與 list.pop() 不同,dict.pop() 傳入的引數為 key 值
print(x)
del y[2] # 與 del list[] 不同,del dict[] 傳入的引數為 key 值
print(y)
執行結果:
b
{1: 'a', 3: 'c', 4: 'd'}
{1: 'a', 3: 'c', 4: 'd'}
dict 以元組為 key 儲存的 value:
x = {(1, 2, 3): "aaa", (4, 5, 6): "bbb"}
a = 1
b = 2
c = 3
print(x[(a, b, c)])
執行結果:
aaa
{ i: j for i, j in iter } 生成字典:
a = (1, 2, 3, 4)
b = ("a", "b", "c", "d")
x = zip(a, b) # zip 打包成一個迭代
print({i: j for i, j in x}) # 生成字典
執行結果:
{1: 'a', 2: 'b', 3: 'c', 4: 'd'}