Python4:DataStructure
生活随笔
收集整理的這篇文章主要介紹了
Python4:DataStructure
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.Introduction
數據結構基本上就是可以處理一些數據的結構。或者說,它們是用來存儲一組相關數據的。在Python中有三種內建的數據結構——列表、元組和字典。2.列表
list是處理一組有序項目的數據結構,即可以在一個列表中存儲一個序列的項目。假想有一個購物列表,上面記載著要買的東西,就容易理解列表了。只不過在你的購物表上,可能每樣東西都獨自占有一行,而在Python中,在每個項目之間用逗號分割。列表中的項目應該包括在方括號中,這樣Python就知道你是在指明一個列表。一旦創建了一個列表,就可以添加、刪除或是搜索列表中的項目。由于可以增加或刪除項目,我們說列表是可變的數據類型,即這種類型是可以被改變的。 #using_list.py #This is my shopping list shoplist =['apple','mango','carrot','banana']; print 'i have', len(shoplist), 'items to purchase';print 'There items are :'; for item in shoplist:print item;print '\n I also have to buy rice.';shoplist.append('rice'); print 'My shopping list is now ', shoplist;print 'I will sort my list now'; shoplist.sort(); print 'Sorted shopping list is ',shoplist;print 'The first item I will buy is',shoplist[0]; olditem = shoplist[0]; del shoplist[0]; print 'I bought the',olditem; print 'My shopping list is now ',shoplist;
3.元組
元組和列表十分類似,只不過元組和字符串一樣是不可變的,即不能修改元組。元組通過圓括號中用逗號分割的項目定義。元組通常用在使語句或用戶定義的函數能夠安全地采用一組值的時候,即被使用的元組的值不會改變。#using_tuple.py 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 the 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];
4.字典
字典類似于通過聯系人名字查找地址和聯系人詳細情況的地址簿,即,把鍵(名字)和值(詳細情況)聯系在一起。注意,鍵必須是唯一的。鍵值對在字典中以這樣的方式標記:d = {key1 : value1, key2 : value2 }。注意它們的鍵/值對用冒
號分割,而各個對用逗號分割,所有這些都包括在花括號中。
記住字典中的鍵/值對是沒有順序的。如果想要一個特定的順序,那么應該在使用前對它們排序。字典是dict類的實例/對象。
#using_dict.py #ab = address book ab = {'Swaroop' :'swaroopch@byteofpython.info','Larry' :'larry@wall.org','Matsumoto':'matz@ruby-lang.org','Spammer' :'spammer@hotmail.com'}; print "Swaroop's address is %s :", ab['Swaroop']; #adding a key/value pair ab['Guido'] = 'guido@python.org';#deleting key/value pair del ab['Spammer']; print '\nThere are %d contacts in the address-book\n' % len(ab);for name, address in ab.items():print 'contact %s at %s' % (name,address);if 'Guido' in ab:print "\nGuido's address is %s" % ab['Guido'];
5.序列
列表、元組和字符串都是序列。序列的兩個主要特點是索引操作符和切片操作符。索引操作符讓我們可以從序列中抓取一個特定項目。切片操作符讓我們能夠獲取序列的一個切片,即一部分序列。#using_sqe.py shoplist = ['apple','mango','carrot','banana'];# Indexing or 'Subscription' operation print 'Item 0 is', shoplist[0] print 'Item 1 is', shoplist[1] print 'Item 2 is', shoplist[2] print 'Item 3 is', shoplist[3] print 'Item -1 is', shoplist[-1] print 'Item -2 is', shoplist[-2]# Slicing on a list print 'Item 1 to 3 is', shoplist[1:3] print 'Item 2 to end is', shoplist[2:] print 'Item 1 to -1 is', shoplist[1:-1] print 'Item start-end is', shoplist[:]# Slicing on a string name = 'swaroop' print 'characters 1 to 3 is', name[1:3] print 'characters 2 to end is', name[2:] print 'characters 1 to -1 is', name[1:-1] print 'characters start-end is', name[:]
6.reference
當創建一個對象并給它賦一個變量的時候,這個變量僅僅引用那個對象,而不是表示這個對象本身!也就是說,變量名指向你計算機中存儲那個對象的內存。這稱作名稱到對象的綁定。一般說來,不需要擔心這個,只是在引用上有些細微的效果需要注意。示例如下:#using_reference.py print 'Simple Assignment' shoplist = ['apple', 'mango', 'carrot', 'banana'] mylist = shoplist # mylist is just another name pointing to the same object! del shoplist[0] print 'shoplist is', shoplist print 'mylist is', mylist # notice that both shoplist and mylist both print the same list without # the 'apple' confirming that they point to the same object print 'Copy by making a full slice' mylist = shoplist[:] # make a copy by doing a full slice del mylist[0] # remove first item print 'shoplist is', shoplist print 'mylist is', mylist # notice that now the two lists are different
7.String
# String_method.py name = 'Swaroop' # This is a string object if name.startswith('Swa'):print 'Yes, the string starts with "Swa"' if 'a' in name:print 'Yes, it contains the string "a"' if name.find('war') != -1:print 'Yes, it contains the string "war"'delimiter = '_*_'mylist = ['Brazil', 'Russia', 'India', 'China']print delimiter.join(mylist)總結
以上是生活随笔為你收集整理的Python4:DataStructure的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python3:module
- 下一篇: 【IPC-钩子】WM_COPYDATA和