日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

Python中3種內(nèi)建數(shù)據(jù)結(jié)構(gòu):列表、元組和字典

系統(tǒng) 1946 0

Python中有3種內(nèi)建的數(shù)據(jù)結(jié)構(gòu):列表、元組和字典。參考簡明Python教程


1. 列表
list是處理一組有序項目的數(shù)據(jù)結(jié)構(gòu),即你可以在一個列表中存儲一個 序列 的項目。假想你有一個購物列表,上面記載著你要買的東西,你就容易理解列表了。只不過在你的購物表上,可能每樣?xùn)|西都獨自占有一行,而在Python中,你在每個項目之間用逗號分割。

列表中的項目應(yīng)該包括在方括號中,這樣Python就知道你是在指明一個列表。一旦你創(chuàng)建了一個列表,你可以添加、刪除或是搜索列表中的項目。由于你可以增加或刪除項目,我們說列表是 可變的 數(shù)據(jù)類型,即這種類型是可以被改變的。
例:

          
#!/usr/bin/env python
#coding:utf8
 
list = ['Linux', 'Nginx', 'MySQL', 'PHP']
 
print 'These items are:',
for item in list:
print item,
 
print '\nadd Apache.'
list.append('Apache')
print 'list is now', list
 
print '\nI will sort my list now'
list.sort()
print 'Sorted list is %s' % list
 
print '\nThe first item ', list[0]
item0 = list[0]
print 'delete first item'
del list[0]
print 'list is now', list
        

輸出

          
$python using_list.py
These items are: Linux Nginx MySQL PHP
add Apache.
list is now ['Linux', 'Nginx', 'MySQL', 'PHP', 'Apache']
 
I will sort my list now
Sorted list is ['Apache', 'Linux', 'MySQL', 'Nginx', 'PHP']
 
The first item Apache
delete first item
list is now ['Linux', 'MySQL', 'Nginx', 'PHP']
        

2. 元組
元組和列表十分類似,只不過元組和字符串一樣是 不可變的 即你不能修改元組。元組通過圓括號中用逗號分割的項目定義。元組通常用在使語句或用戶定義的函數(shù)能夠安全地采用一組值的時候,即被使用的元組的值不會改變。
例:

          
#!/usr/bin/env python
#coding:utf8
 
zoo = ('wolf', 'elephant', 'penguin')
print 'Number of animals in the zoo is', len(zoo)
 
new_zoo = ('monkey', 'dolphin', zoo)
print 'Number of animals in the new zoo is', len(new_zoo)
print 'All animals in new zoo are', new_zoo
print 'Animals brought from old zoo are', new_zoo[2]
print 'Last animal brought from old zoo is', new_zoo[2][2]
        

輸出

          
$ python using_tuple.py
Number of animals in the zoo is 3
Number of animals in the new zoo is 3
All animals in new zoo are ('monkey', 'dolphin', ('wolf', 'elephant', 'penguin'))
Animals brought from old zoo are ('wolf', 'elephant', 'penguin')
Last animal brought from old zoo is penguin
        

3. 字典
字典類似于你通過聯(lián)系人名字查找地址和聯(lián)系人詳細情況的地址簿,即,我們把鍵(名字)和值(詳細情況)聯(lián)系在一起。注意,鍵必須是唯一的,就像如果有兩個人恰巧同名的話,你無法找到正確的信息。

注意,你只能使用不可變的對象(比如字符串)來作為字典的鍵,但是你可以不可變或可變的對象作為字典的值。基本說來就是,你應(yīng)該只使用簡單的對象作為鍵。

鍵值對在字典中以這樣的方式標記:d = {key1 : value1, key2 : value2 }。注意它們的鍵/值對用冒號分割,而各個對用逗號分割,所有這些都包括在花括號中。

記住字典中的鍵/值對是沒有順序的。如果你想要一個特定的順序,那么你應(yīng)該在使用前自己對它們排序。

字典是dict類的實例/對象。
例:

          
#!/usr/bin/env python
#coding:utf8
 
contacts = { 'Admin' : 'admin@jb51.net',
'Linuxeye' : 'linuxeye@jb51.net',
'Support' : 'support@jb51.net'
}
 
print "Linuxeye's address is %s" % contacts['Linuxeye']
 
# Adding a key/value pair
contacts['test'] = 'test@jb51.net'
 
# Deleting a key/value pair
del contacts['Support']
 
print '\nThere are %d contacts in the address-book\n' % len(contacts)
for name, address in contacts.items():
print 'Contact %s at %s' % (name, address)
 
if contacts.has_key('test'):
print "\ntest's address is %s" % contacts['test']
        

輸出

          
$ python using_dict.py
Linuxeye's address is linuxeye@jb51.net
 
There are 3 contacts in the address-book
 
Contact Admin at admin@jb51.net
Contact test at test@jb51.net
Contact Linuxeye at linuxeye@jb51.net
 
test's address is test@jb51.net
        


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 柯坪县| 武邑县| 绿春县| 灌阳县| 隆化县| 永顺县| 石棉县| 甘泉县| 札达县| 乌鲁木齐县| 蚌埠市| 铜陵市| 志丹县| 五常市| 高雄市| 八宿县| 江阴市| 高雄县| 华亭县| 昌邑市| 静安区| 武隆县| 柳河县| 巴东县| 三原县| 仙游县| 巴南区| 通海县| 城口县| 绵阳市| 嘉鱼县| 恩施市| 江陵县| 砀山县| 上蔡县| 临桂县| 霍州市| 三河市| 武夷山市| 威海市| 林西县|