python 中经常用到的数据结构.docx
- 文档编号:10549794
- 上传时间:2023-02-21
- 格式:DOCX
- 页数:7
- 大小:16.94KB
python 中经常用到的数据结构.docx
《python 中经常用到的数据结构.docx》由会员分享,可在线阅读,更多相关《python 中经常用到的数据结构.docx(7页珍藏版)》请在冰豆网上搜索。
python中经常用到的数据结构
python中经常用到的数据结构
1.列表
#!
/usr/bin/python
#Filename:
using_list.py##################################
defshowShopList(shoplist):
foriteminshoplist:
printitem,
#################################shoplist=['apple','mango','carrot','banana']print'Ihave',len(shoplist),'itemstopurchase.'
print'Theseitemsare:
',#Noticethecommaattheendoftheline
showShopList(shoplist)print'\nIalsohavetobuyrice.'
shoplist.append('rice')
print'Myshoppinglistisnow:
',showShopList(shoplist)print'Iwillsortmyshoplist'
shoplist.sort()
print'Sortedshoppinglistis:
',showShopList(shoplist)
print'ThefirstitemIwillbuyis',shoplist[0]
olditem=shoplist[0]
delshoplist[0]
print'Iboughtthe',olditemprint'NowIhave',len(shoplist),'itemstopurchase'
print'Myshoppinglistisnow:
',showShopList(shoplist)
print'TheseconditemIwillbuyis',shoplist[0]
root@sgb-desktop:
~/program#pythonusing_list.py
Ihave4itemstopurchase.
Theseitemsare:
applemangocarrotbanana
Ialsohavetobuyrice.
MyshoppinglistisnowapplemangocarrotbananariceNone
Iwillsortmyshoplist
SortedshoppinglistisapplebananacarrotmangoriceNone
ThefirstitemIwillbuyisapple
Iboughttheapple
NowIhave4itemstopurchase
MyshoppinglistisnowbananacarrotmangoriceNone
TheseconditemIwillbuyisbanana
2.元组
元组和列表十分类似,只不过元组和字符串一样是不可变的即你不能修改元组。
元组通过圆括号中用逗号分割的项目定义。
元组通常用在使语句或用户定义的函数能够安全地采用一组值的时候,即被使用的元组的值不会改变。
元组最通常的用法是用在打印语句中,下面是一个例子:
#!
/usr/bin/python
#coding:
utf-8#为了在此文件中包含中文
#Filename:
print_tuple.pyage=22
name='Swaroop'print'%sis%dyearsold'%(name,age)#括号以及其内的数据合称之为元>组
print'Whyis%splayingwiththatpython?
'%name#单个字符同样可以使用此格式
这个有点类似于C的输出,不过python中好像给括号多了个定义,叫元组
3.字典
字典类似于你通过联系人名字查找地址和联系人详细情况的地址簿,即,我们把键(名字)和值(详细情况)联系在一起。
注意,键必须是唯一的,就像如果有两个人恰巧同名的话,你无法找到正确的信息。
注意,你只能使用不可变的对象(比如字符串)来作为字典的键,但是你可以不可变或可变的对象作为字典的值。
基本说来就是,你应该只使用简单的对象作为键
#!
/usr/bin/python
#Filename:
using_dict.py#'ab'isshortfor'a'ddress'b'ookab={
'Swaroop':
'Swaroopch@byteofpython.info',
'Larry':
'larry@wall.org',
'Matsumoto':
'matz@ruby-lang.org',
'Spammer':
'spammer@'
}print'Swaroop\'saddressis%s'%ab['Swaroop']#Addingakey/valuepair
ab['Guido']='guido@python.org'#Deletingakey/valuepair
delab['Spammer']print'\nThereare%dcontactsintheaddress-book\n'%len(ab)
forname,addressinab.items():
print'Contact%sat%s'%(name,address)#if'Guido'inab:
#print'ContactGuidoat%s'%ab['Guido']ifab.has_key('Guido'):
print'ContactGuidoat%s'%ab['Guido']
root@sgb-desktop:
~/program#pythonusing_dict.py
Swaroop'saddressisSwaroopch@byteofpython.infoThereare4contactsintheaddress-bookContactSwaroopatSwaroopch@byteofpython.info
ContactMatsumotoatmatz@ruby-lang.org
ContactLarryatlarry@wall.org
ContactGuidoatguido@python.org
ContactGuidoatguido@python.org
root@sgb-desktop:
~/program#geditusing_dict.py
这个似乎和PHP中的数组很像
4.序列
列表、元组和字符串都是序列,但是序列是什么,它们为什么如此特别呢?
序列的两个主要特点是索引操作符和切片操作符。
索引操作符让我们可以从序列中抓取一个特定项目。
切片操作符让我们能够获取序列的一个切片,即一部分序列。
#!
/usr/bin/python
#Filename:
seq.pyshoplist=['apple','mango','carrot','banana']#Indexingor'Subscription'operation
print'Item0is',shoplist[0]
print'Item1is',shoplist[1]
print'Item2is',shoplist[2]
print'Item3is',shoplist[3]
print'Item-1is',shoplist[-1]
print'Item-2is',shoplist[-2]#Slicingonalist
print'Item1to3is',shoplist[1:
3]
print'Item2toendis',shoplist[2:
]
print'Item1to-1is',shoplist[1:
-1]
print'Itemstarttoendis',shoplist[:
]#Slicingonastring
name='swaroop'
print'characters1to3is',name[1:
3]
print'characters2toendis',name[2:
]
print'characters1to-1is',name[1:
-1]
print'charactersstarttoendis',name[:
]
$pythonseq.py
Item0isapple
Item1ismango
Item2iscarrot
Item3isbanana
Item-1isbanana
Item-2iscarrot
Item1to3is['mango','carrot']
Item2toendis['carrot','banana']
Item1to-1is['mango','carrot']
Itemstarttoendis['apple','mango','carrot','banana']
characters1to3iswa
characters2toendisaroop
characters1to-1iswaroo
charactersstarttoendisswaroop
注:
这个挺有意思的,估计以后会经常用到
5.对象与参考
当你创建一个对象并给它赋一个变量的时候,这个变量仅仅参考那个对象,而不是表示这个对象本身!
也就是说,变量名指向你计算机中存储那个对象的内存。
这被称作名称到对象的绑定。
#!
/usr/bin/python
#Filename:
referance.pyprint'SimpleAssignment'
shoplist=['apple','mango','carrot','banana']
mylist=shoplistdelshoplist[0]print'shoplistis',shoplist
print'mylisyis',mylist
#noticethatbothmylistandshoplistbothprintthesamelistwithout
#the'apple'confirmingthattheypointtothesameobjectprint'Copybymakingafullslice'
mylist=shoplist[:
]#makeacopybydoingfullslice
delmylist[0]#removefirstitemprint'shoplistis',shoplist
print'mylistis',mylist
#noticethatnowthetwolistsaredeferent
root@sgb-desktop:
~/program#pythonreferancee.py
SimpleAssignment
shoplistis['mango','carrot','banana']
mylisyis['mango','carrot','banana']
Copybymakingafullslice
shoplistis['mango','carrot','banana']
mylistis['carrot','banana']
大多数解释已经在程序的注释中了。
你需要记住的只是如果你想要复制一个列表或者类似的序列或者其他复杂的对象(不是如整数那样的简单对象),那么你必须使用切片操作符来取得拷贝。
如果你只是想要使用另一个变量名,两个名称都参考同一个对象,那么如果你不小心的话,可能会引来各种麻烦。
6.字符串也是对象
#!
/usr/bin/python
#_*_coding:
utf-8_*_
#Filename:
str_methods.pyname='Swaroop'#Thisisastringobjectifname.startswith('Swar'):
print'Yes,thestringstartswith"Swar"'
if'a'inname:
print'Yes,itcontainsthestring"a"'
ifname.find('war')!
=-1:
print'Yes,itcontainsthestring"war"'
delimiter='_*_'mylist=['Brazil','Russia','India','China']
printdelimiter.join(mylist)#join联接
root@sgb-desktop:
~/program#pythonstr_methonds.py
Yes,thestringstartswith"Swar"
Yes,itcontainsthestring"a"
Yes,itcontainsthestring"war"
Brazil_*_Russia_*_India_*_China
这里,我们看到使用了许多字符串方法。
startwith方法是用来测试字符串是否以给定字符串开始。
in操作符用来检验一个给定字符串是否为另一个字符串的一部分。
find方法用来找出给定字符串在另一个字符串中的位置,或者返回-1以表示找不到子字符串。
str类也有以一个作为分隔符的字符串join序列的项目的整洁的方法,它返回一个生成的大字符串。
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- python 中经常用到的数据结构 经常 用到 数据结构