Python基礎學習程式碼之映像集合
def func1(): dict1 = {} dict2 = {'name':'earth','port':80} return dict1,dict2 def func2(): return dict((['x',1],['y',2])) def func3(): adict = {}.fromkeys(['x','y'],23) return adict def func4(): alist = {'name':'earth','port':80} for keys in alist.keys(): print "%s %s" % (keys,alist[keys]) def func5(): alist = {'name':'earth','port':80} for keys in alist: print "%s %s" % (keys,alist[keys]) def func6(akey): alist = {'name':'earth','port':80} if akey in alist: return True else: return False def func7(akey): alist = {'name':'earth','port':80} if alist.has_key(akey): return True else: return False def func8(): alist = {'name':'earth','port':80} print 'host %(name)s is running on %(port)d' % alist def func9(akey): alist = {'name':'earth','port':80} if akey in alist: del alist[akey] return True else: return False def func10(): alist = {'name':'earth','port':80} alist.clear() return alist def func11(): alist = {'name':'earth','port':80} del alist def func12(akey): alist = {'name':'earth','port':80} if akey in alist: return alist.pop(akey) def func13(): alist = {'name':'earth','a':80} blist = {'name':'earth','z':8080} return cmp(alist,blist) def func14(): alist = zip(('x','y'),(1,2)) blist = dict([('xy'[i-1],i) for i in range(1,3)]) return dict(alist),blist def func15(): adict = {'name':'earth','port':80} bdict = adict.copy() return bdict,len(bdict) def func16(): adict = {'name':'earth','port':80} bdict = {'name':'earth','port':8080} print adict.keys() print adict.values() print adict.items() adict.update(bdict) print adict def func17(): adict = {'name':'earth','port':80} for keys in sorted(adict): print 'adict %s has value %s' % (keys,adict[keys]) def func18(akey): adict = {'name':'earth','port':80} if adict.get(akey): return True else: return 'no such key!' def func19(): adict = {'name':'earth','port':80} bdict = {}.fromkeys('abc') print bdict return adict.setdefault('name','wycqhost') import time nowtime = time.time() def gettime(nowtime): return time.strftime('%Y/%m/%d %H:%M:%S',time.localtime(nowtime)) login = {} def newuser(): prompt = 'login desired:' name = '' while True: name = raw_input(prompt) if login.has_key(name): prompt = 'name taken,try another:' continue else: break pwd = raw_input('passwd:') login[name] = [abs(hash(pwd))] login[name].append(0) print login def olduser(): nowtime = time.time() name = raw_input('login:') if name not in login: choose = raw_input('will you create a new user(y/n)') if choose.lower()[0] == 'y': newuser() else: pass showmenu() else: pwd = raw_input('passwd:') passwd = login.get(name)[0] if abs(hash(passwd)) == abs(hash(pwd)): if login[name][1] == 0: print login print 'welcome back',name,'you first time loggin' else: print 'welcome back',name if nowtime - login[name][1] <= 14400: print 'you are already logged at time',gettime(login[name][1]) login[name][1] = nowtime else: print 'login incorrect' return def showuser(): print 'show all user:' for user in login.keys(): print user def deleteuser(): duser = raw_input('delete user:').lower() if duser in login.keys(): del login[duser] else: print 'user %s is not exists' % duser showmenu() def showmenu(): prompt = ''' (n)ew user login (o)ld user login (s)how all user (d)elete user (q)uit enter choice: ''' done = False while not done: chosen = False while not chosen: try: choice = raw_input(prompt).strip()[0].lower() except (EOFError,KeyboardInterrupt): choice = 'q' print 'you picked %s' % choice if choice not in 'noqds': print 'invalid option,try again' else: chosen = True if choice == 'q': done = True if choice == 'n': newuser() if choice == 'o': olduser() if choice == 'd': deleteuser() if choice == 's': showuser() #if __name__ == '__main__': # showmenu() def func20(): str1 = '093keffeoelgn' t = set(str1) s = frozenset(str1) return t == s def func21(): aset = set('xiewenbin') if 'x' in aset: print "x in aset" def func22(): aset = set('strings') aset.add('http') aset.update('httpx') aset.remove('http') aset -= set('x') for i in aset: print i del aset def func23(): aset = set('abc') bset = set('abcedf') return aset <= bset def func24(): aset = set('markshop') bset = frozenset('earthshop') print aset | bset print bset & aset print aset ^ bset print aset - bset def func25(): s = set('cheeseshop') u = frozenset(s) s |= set('xie') print s s = set(u) s &= set('shop') print s s = set(u) s -= set('shop') print s s = set(u) t = frozenset('bookshop') s ^= t print s print len(s) import os def func26(): frozenset(['a','b','c']) f = open('test.txt','w') for i in range(5): f.write('%d\n'%i) f.close() f = open('test.txt','r') print set(f) f.close() os.remove('test.txt') def func27(): alist = ['a','b'] blist = [1,2] print dict(zip(alist,blist)) def func28(): adict = {'a':1,'b':2,'c':3} bdict = {} for keys in adict.keys(): bdict[adict[keys]] = keys return bdict def func29(sstr,dstr,string,casemap=True): assert len(sstr) >= len(dstr) table = dict(zip(sstr,dstr)) print table if len(sstr) > len(dstr): temp = {}.fromkeys(sstr[len(dstr)]) table.update(temp) print table ls = [] for ch in string: if not casemap: if ch.lower() in table: ls.append(table[ch.lower()]) elif ch.upper() in table: ls.append(table[ch.upper()]) else: ls.append(ch) continue if ch in table: ls.append(table[ch]) else: ls.append(ch) ls = [ch for ch in ls if ch] print ls return " ".join(ls) def func30(sstr): alist = [chr((num + 13) % 26 + ord('a')) for num in range(26)] blist = [chr(num + ord('a')) for num in range(26)] table = dict(zip(blist,alist)) astr = "".join(alist).upper() bstr = "".join(blist).upper() table.update(dict(zip(bstr,astr))) ls = [] for ch in sstr: if ch in table: ls.append(table[ch]) else: ls.append(ch) return " ".join(ls) import random def func31(): alist = [random.randint(i,10) for i in range(10)] blist = [random.randint(i,10) for i in range(10)] aset = set(alist) bset = set(blist) print aset | bset print aset & bset
以上就是Python基礎學習程式碼之映像集合的內容,更多相關內容請關注PHP中文網(www.php.cn)!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

Python更易學且易用,C 則更強大但複雜。 1.Python語法簡潔,適合初學者,動態類型和自動內存管理使其易用,但可能導致運行時錯誤。 2.C 提供低級控制和高級特性,適合高性能應用,但學習門檻高,需手動管理內存和類型安全。

要在有限的時間內最大化學習Python的效率,可以使用Python的datetime、time和schedule模塊。 1.datetime模塊用於記錄和規劃學習時間。 2.time模塊幫助設置學習和休息時間。 3.schedule模塊自動化安排每週學習任務。

Python在開發效率上優於C ,但C 在執行性能上更高。 1.Python的簡潔語法和豐富庫提高開發效率。 2.C 的編譯型特性和硬件控制提升執行性能。選擇時需根據項目需求權衡開發速度與執行效率。

每天學習Python兩個小時是否足夠?這取決於你的目標和學習方法。 1)制定清晰的學習計劃,2)選擇合適的學習資源和方法,3)動手實踐和復習鞏固,可以在這段時間內逐步掌握Python的基本知識和高級功能。

Python和C 各有優勢,選擇應基於項目需求。 1)Python適合快速開發和數據處理,因其簡潔語法和動態類型。 2)C 適用於高性能和系統編程,因其靜態類型和手動內存管理。

pythonlistsarepartofthestAndArdLibrary,herilearRaysarenot.listsarebuilt-In,多功能,和Rused ForStoringCollections,而EasaraySaraySaraySaraysaraySaraySaraysaraySaraysarrayModuleandleandleandlesscommonlyusedDduetolimitedFunctionalityFunctionalityFunctionality。

Python在自動化、腳本編寫和任務管理中表現出色。 1)自動化:通過標準庫如os、shutil實現文件備份。 2)腳本編寫:使用psutil庫監控系統資源。 3)任務管理:利用schedule庫調度任務。 Python的易用性和豐富庫支持使其在這些領域中成為首選工具。

Python在Web開發中的關鍵應用包括使用Django和Flask框架、API開發、數據分析與可視化、機器學習與AI、以及性能優化。 1.Django和Flask框架:Django適合快速開發複雜應用,Flask適用於小型或高度自定義項目。 2.API開發:使用Flask或DjangoRESTFramework構建RESTfulAPI。 3.數據分析與可視化:利用Python處理數據並通過Web界面展示。 4.機器學習與AI:Python用於構建智能Web應用。 5.性能優化:通過異步編程、緩存和代碼優
