1. 程式人生 > 實用技巧 >python小技巧

python小技巧

#coding:utf-8


# 知識點一:將2個字典合成一個字典
dict1={'小顏':18,'小小':"20",'小A':48}
dict2={'小酒':18,"MM":24,'小小':24}
dict3={**dict1,**dict2,'小米':'ss'}
print(dict3)
dict3={**dict2,**dict1}
print(dict3)

輸出:



#知識點二:對字典進行排序

dict1={0:18,3:20,2:48,5:20}
#對字典得鍵進行排序
print(sorted(dict1.keys()))
#對字典得值進行排序
print(sorted(dict1.values()))
#對字典得鍵值對進行排序
#需要先將鍵值對轉換成元組,再對元組進行排序
#但此時預設是對元組得第一個元素進行排序
print(sorted(dict1.items()))
#修改成對元素得第2個元素進行排序
print(sorted(dict1.items(), key=lambda x:x[1],reverse=True))
#排序完成,需要將其修改回字典
print({k:v for k,v in sorted(dict1.items(), key=lambda x:x[1],reverse=True)})

輸出:



#知識點三:使用in, not in表示包含關係
a='This is a book'
b='book'
print(b in a)
print(b not in a)

輸出:



#知識點四:extend和append的區別
#extend表示拆包後新增進去
a=[1,2,3,4]
b=[5,6]
a.append(b)
print(a)#輸出[1, 2, 3, 4, [5, 6]]
a=[1,2,3]
a.extend(b)
print(a)#輸出[1, 2, 3, 5, 6]
#print(d)

輸出:



#知識點五:深拷貝和淺拷貝
import copy#深拷貝需要匯入copy
a=[1,2,3,4,[1,2]]
b=a.copy()
c=copy.deepcopy(a)
a[4].append(4)
print(b)# 當a改變時b也跟著改變,ab獨立,但是他們的子物件指同一個記憶體空間。輸出:[1, 2, 3, 4, [1, 2, 4]]
print(c)#當a改變時c不會改變,完全拷貝,完全獨立。輸出:[1, 2, 3, 4, [1, 2]]
輸出:




#知識點六:dir 可以知道物件得所有方法
name='lxh'
print(name.upper())#轉換成全大寫字元
print(dir(name))#查詢name得所有方法

輸出:['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

#知識點七:pprint,輸出的字元格式變得好看點
import pprint
pprint.pprint(dir(str))

輸出:

['__add__',
'__class__',
'__contains__',
'__delattr__',
'__dir__',
'__doc__',
'__eq__',
'__format__',
'__ge__',
'__getattribute__',
'__getitem__',
'__getnewargs__',
'__gt__',
'__hash__',
'__init__',
'__init_subclass__',
'__iter__',
'__le__',
'__len__',
'__lt__',
'__mod__',
'__mul__',
'__ne__',
'__new__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__rmod__',
'__rmul__',
'__setattr__',
'__sizeof__',
'__str__',
'__subclasshook__',
'capitalize',
'casefold',
'center',
'count',
'encode',
'endswith',
'expandtabs',
'find',
'format',
'format_map',
'index',
'isalnum',
'isalpha',
'isascii',
'isdecimal',
'isdigit',
'isidentifier',
'islower',
'isnumeric',
'isprintable',
'isspace',
'istitle',
'isupper',
'join',
'ljust',
'lower',
'lstrip',
'maketrans',
'partition',
'replace',
'rfind',
'rindex',
'rjust',
'rpartition',
'rsplit',
'rstrip',
'split',
'splitlines',
'startswith',
'strip',
'swapcase',
'title',
'translate',
'upper',
'zfill']



#知識點八:zip組合列表
names=['羅一一','已的','小心','丫丫']
score=[99,87,76,88]
#迴圈輸出人名和對應的成績
for name,score in zip(names,score):
print(name,score)

輸出:



#知識點九:三元運算子
x=23

print('偶數') if x%2==0 else print('奇數')


輸出:奇數