深刻MySQL存儲引擎比擬的詳解。本站提示廣大學習愛好者:(深刻MySQL存儲引擎比擬的詳解)文章只能為提供參考,不一定能成為您想要的結果。以下是深刻MySQL存儲引擎比擬的詳解正文
Python字典的創立
辦法一:
>>> blank_dict = {} >>> product_dict = {'MAC':8000,'Iphone':5000, 'ipad':4000, 'mp3': 300} >>> product_dict {'ipad': 4000, 'MAC': 8000, 'Iphone': 5000, 'mp3': 300} >>> blank_dict,product_dict ({}, {'ipad': 4000, 'MAC': 8000, 'Iphone': 5000, 'mp3': 300})
辦法二:
從Python 2.2版本開端
>>> fdict = dict((['www','www.linuxeye.com'],['blog','blog.linuxeye.com'])) >>> fdict {'blog': 'blog.linuxeye.com', 'www': 'www.linuxeye.com'}
辦法三:
從Python 2.3版本開端, 可以用一個很便利的內建辦法fromkeys() 來創立一個”默許”字典, 字典中元素具有雷同的值 (假如沒有給出, 默許為None):
>>> fk_dict = {}.fromkeys(('a','b'),'LinuxEye') >>> fk_dict {'a': 'LinuxEye', 'b': 'LinuxEye'} >>> fk2_dict = {}.fromkeys(('yeho','python')) >>> fk2_dict {'python': None, 'yeho': None} >>> fk3_dict = {}.fromkeys('yeho','python') >>> fk3_dict {'y': 'python', 'h': 'python', 'e': 'python', 'o': 'python'}
Python字典查、增、刪、改
>>> product_dict = {'MAC':8000,'Iphone':5000, 'mp3': 300} >>> product_dict['Iphone'] 5000 >>> product_dict['ipad'] = 4000 #增 >>> product_dict {'ipad': 4000, 'MAC': 8000, 'Iphone': 5000, 'mp3': 300} >>> product_dict.keys() #檢查索引 ['ipad', 'MAC', 'Iphone', 'mp3'] >>> product_dict.values() #檢查值 [4000, 8000, 5000, 300] >>> product_dict.items() [('ipad', 4000), ('MAC', 8000), ('Iphone', 5000), ('mp3', 300)] >>> for product,price in product_dict.items(): ... print product,price ... ipad 4000 MAC 8000 Iphone 5000 mp3 300 >>> product_dict.has_key('Iphone') #斷定key能否存在 True >>> product_dict.has_key('Itouch') False >>> product_dict.pop('MAC') #刪除指定key和value 8000 >>> product_dict {'ipad': 4000, 'Iphone': 5000, 'mp3': 300} >>> product_dict.popitem() #刪除第一個key和value ('ipad', 4000) >>> product_dict {'Iphone': 5000, 'mp3': 300} >>> product_dict = {'Iphone': 5000, 'mp3': 300} >>> del product_dict['Iphone'] #用del函數刪除指定key和value >>> product_dict {'mp3': 300} >>> product_dict['mp3'] = 299 #改 >>> product_dict {'mp3': 299} >>> product_dict.clear() #清空字典內容(空字典) >>> product_dict {} >>> del product_dict #刪除字典 >>> product_dict = {'mp3': 300} >>> del product_dict #已刪除報錯 Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'product_dict' is not defined