一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Python - Python中3種內建數據結構:列表、元組和字典

Python中3種內建數據結構:列表、元組和字典

2020-05-17 11:03腳本之家 Python

這篇文章主要介紹了Python中3種內建數據結構:列表、元組和字典,需要的朋友可以參考下

Python中有3種內建的數據結構:列表、元組和字典。參考簡明Python教程


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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/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

輸出

?
1
2
3
4
5
6
7
8
9
10
11
$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. 元組
元組和列表十分類似,只不過元組和字符串一樣是 不可變的 即你不能修改元組。元組通過圓括號中用逗號分割的項目定義。元組通常用在使語句或用戶定義的函數能夠安全地采用一組值的時候,即被使用的元組的值不會改變。
例:

?
1
2
3
4
5
6
7
8
9
10
11
#!/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]

輸出

?
1
2
3
4
5
6
$ 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. 字典
字典類似于你通過聯系人名字查找地址和聯系人詳細情況的地址簿,即,我們把鍵(名字)和值(詳細情況)聯系在一起。注意,鍵必須是唯一的,就像如果有兩個人恰巧同名的話,你無法找到正確的信息。

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

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

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/usr/bin/env python
#coding:utf8
 
contacts = { 'Admin' : '[email protected]',
'Linuxeye' : '[email protected]',
'Support' : '[email protected]'
}
 
print "Linuxeye's address is %s" % contacts['Linuxeye']
 
# Adding a key/value pair
contacts['test'] = '[email protected]'
 
# 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']

輸出

?
1
2
3
4
5
6
7
8
9
10
$ python using_dict.py
Linuxeye's address is [email protected]
 
There are 3 contacts in the address-book
 
Contact Admin at [email protected]
Contact test at [email protected]
Contact Linuxeye at [email protected]
 
test's address is [email protected]

 

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 青草娱乐极品免费视频 | 美女69xx | 国产福利自产拍在线观看 | 国产极品精频在线观看 | 性欧美高清强烈性视频 | 無码一区中文字幕少妇熟女网站 | 国产一区日韩二区欧美三区 | 久久久91精品国产一区二区 | 四虎精品视频在线永久免费观看 | 亚洲精品国产自在现线最新 | 色综合天天综合网站中国 | 免费超级乱淫视频播放性 | 99ri国产精品| 国产麻豆传媒在线观看 | 男人天堂bt| 极品美女a∨片在线看 | 午夜影院0606 | 俄罗斯引擎首页进入 | 亚洲国产日韩制服在线观看 | 国产在线视频色综合 | 国产精品视频免费视频 | 热剧库 | 亚洲视频中文 | 91桃花 | 成人免费一区二区三区在线观看 | 精品国内自产拍在线视频 | 天堂在线看 | 嗯啊在线观看免费影院 | 亚洲精品国产在线 | 九九精品国产亚洲A片无码 九九99热久久999精品 | 四虎免费看 | 黑人巨荃大战乌克兰美女 | 国产精品模特hd在线 | 四虎成人免费视频 | 成年性午夜免费视频网站不卡 | 青青国产成人久久91网 | 亚洲国产日韩欧美一区二区三区 | 欧美一区二区三区四区五区六区 | 538免费精品视频搬运工 | 精品一区二区三区高清免费观看 | 久久xxxx |