免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
編程小竅門:15 個(gè)提高效率的 Python 編程技巧

作者:George Seif

英文原文:https://medium.com/@george.seif94/15-python-tips-and-tricks-so-you-dont-have-to-look-them-up-on-stack-overflow-90cec02705ae

1. 交換值
x, y = 1, 2print(x, y)x, y = y, xprint(x, y)
2. 字符串列表合并為一個(gè)字符串
sentence_list = ['my', 'name', 'is', 'George']sentence_string = ' '.join(sentence_list)print(sentence_string)
3. 將字符串拆分為子字符串列表
sentence_string = 'my name is George'sentence_string.split()print(sentence_string)
4. 通過數(shù)字填充初始化列表
[0]*1000 # List of 1000 zeros [8.2]*1000 # List of 1000 8.2's
5. 字典合并
x = {'a': 1, 'b': 2}y = {'b': 3, 'c': 4}z = {**x, **y}
6. 反轉(zhuǎn)字符串
name = 'George'name[::-1]
7. 從函數(shù)返回多個(gè)值
def get_a_string(): a = 'George' b = 'is' c = 'cool' return a, b, csentence = get_a_string()(a, b, c) = sentence
8. 列表解析式
a = [1, 2, 3]b = [num*2 for num in a] # Create a new list by multiplying each element in a by 2
9. 遍歷字典
m = {'a': 1, 'b': 2, 'c': 3, 'd': 4} for key, value in m.items(): print('{0}: {1}'.format(key, value))
10. 同時(shí)遍歷列表的索引和值
m = ['a', 'b', 'c', 'd']for index, value in enumerate(m):  print('{0}: {1}'.format(index, value))
11. 初始化空容器
a_list = list()a_dict = dict()a_map = map()a_set = set()
12. 刪除字符串兩端的無用字符
name = '  George 'name_2 = 'George///'name.strip() # prints 'George'name_2.strip('/') # prints 'George'
13. 列表中出現(xiàn)最多的元素
test = [1, 2, 3, 4, 2, 2, 3, 1, 4, 4, 4]print(max(set(test), key = test.count))
14. 檢查對象的內(nèi)存使用情況
import sysx = 1print(sys.getsizeof(x))
15. 將 dict 轉(zhuǎn)換為 XML
from xml.etree.ElementTree import Elementdef dict_to_xml(tag, d): ''' Turn a simple dict of key/value pairs into XML ''' elem = Element(tag) for key, val in d.items(): child = Element(key) child.text = str(val) elem.append(child) return elem
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
13個(gè)提升碼速的python小技巧
Python開發(fā)的10個(gè)小貼士 – 碼農(nóng)網(wǎng)
Python學(xué)習(xí)教程:Day12-使用正則表達(dá)式
Python 字符串處理備忘單
Python基礎(chǔ)知識點(diǎn)總結(jié)
Python入門之一
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服