`
thecloud
  • 浏览: 880622 次
文章分类
社区版块
存档分类
最新评论

《简明python教程》总结(四)-- 数据结构

阅读更多
数据结构基本上就是——它们是可以处理一些数据的结构 。或者说,它们是用来存储一组相关数据的。
在Python中有三种内建的数据结构——列表、元组和字典
列表(list)
列表中的项目应该包括在方括号中,这样Python就知道是在指明一个列表。
列表可以进行添加、删除或是搜索列表中的项目。说列表是可变的数据类型,即这种类型是可以被改变的。
常用于数据需要改变(删/增)的类型,如购物篮
示例:
#!/usr/bin/python
# Filename: using_list.py
# This is my shopping list
shoplist = ['apple', 'mango', 'carrot', 'banana']
print 'I have', len(shoplist),'items to purchase.'
print 'These items are:', # Notice the comma at end of the line
for item in shoplist:
print item,
print '\nI 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
输出
$ python using_list.py
I have 4 items to purchase.
These items are: apple mango carrot banana
I also have to buy rice.
My shopping list is now ['apple', 'mango', 'carrot', 'banana', 'rice']
I will sort my list now
Sorted shopping list is ['apple', 'banana', 'carrot', 'mango', 'rice']
The first item I will buy is apple
I bought the apple
My shopping list is now ['banana', 'carrot', 'mango', 'rice']
学习:列表类的几个函数 append(), sort(), list.[index]
元组
和列表十分类似,只不过元组和字符串一样是不可变的即你不能修改元组。元组通过圆括号中用逗号分割的项目定义。
通常用在使语句或用户定义的函数能够安全地采用一组值的时候,只是访问,不需要改变。即被使用的元组的值不会改变。例如 利用元组完成打印语句
示例:
#!/usr/bin/python
# Filename: 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 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]
age = 22
name = 'Swaroop'
print '%s is %d years old' % (name, age)
print 'Why is %s playing with that python?' % name
输出
$ 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
Swaroop is 22 years old
Why is Swaroop playing with that python?
分析:后4句为利用元组完成打印
print语句可以使用跟着%符号的项目元组的字符串。这些字符串具备定制的功能。定制让输出满足某种特定的格式。定制可以是%s表示字符串或%d表示整数。元组必须按照相同的顺序来对应这些定制
字典(dict)
字典类似于通过联系人名字查找地址和联系人详细情况的地址簿,即,把键(名字)和值(详细情况)联系在一起。注意,键必须是唯一的,就像如果有两个人恰巧同名的话,无法找到正确的信息。
注意,只能使用不可变的对象(比如字符串)来作为字典的键,但是可以把不可变或可变的对象作为字典的值。基本说来就是,应该只使用简单的对象作为键。
键值对在字典中以这样的方式标记:d = {key1 : value1, key2 : value2 }。注意它们的键/值对用冒号分割,而各个对用逗号分割,所有这些都包括在花括号中。
记住字典中的键/值对是没有顺序的。如果想要一个特定的顺序,那么应该在使用前自己对它们排序。
示例:
#!/usr/bin/python
# Filename: using_dict.py
# 'ab' is short for 'a'ddress'b'ook
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 a 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: # OR ab.has_key('Guido')
print "\nGuido's address is %s" % ab['Guido']
输出
$ python using_dict.py
Swaroop's address is swaroopch@byteofpython.info
There are 4 contacts in the address-book
Contact Swaroop at swaroopch@byteofpython.info
Contact Matsumoto at matz@ruby-lang.org
Contact Larry at larry@wall.org
Contact Guido at guido@python.org
Guido's address is guido@python.org
序列
列表、元组和字符串都是序列。
序列的两个主要特点是索引操作符切片操作符。索引操作符让我们可以从序列中抓取一个特定项目。切片操作符让我们能够获取序列的一个切片,即一部分序列。
切片操作符是序列名后跟一个方括号,方括号中有一对可选的数字,并用冒号分割。记住数是可选的,而冒号是必须的
切片操作符中的第一个数(冒号之前)表示切片开始的位置,第二个数(冒号之后)表示切片到哪里结束。如果不指定第一个数,Python就从序列首开始。如果没有指定第二个数,则Python会停止在序列尾。
注意,返回的序列从开始位置 开始 ,刚好在结束 置之前结束。即开始位置是包含在序列切片中的,而结束位置被排斥在切片外
这样,shoplist[1:3]返回从位置1开始,包括位置2,但是停止在位置3的一个序列切片,因此返回一个含有两个项目的切片。类似地,shoplist[:]返回整个序列的拷贝。
你可以用负数做切片。负数用在从序列尾开始计算的位置。例如,shoplist[:-1]会返回除了最后一个项目外包含所有项目的序列切片。
示例:
#!/usr/bin/python
# Filename: seq.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 to 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 to end is', name[:]
输出
$ python seq.py
Item 0 is apple
Item 1 is mango
Item 2 is carrot
Item 3 is banana
Item -1 is banana
Item -2 is carrot
Item 1 to 3 is ['mango', 'carrot']
Item 2 to end is ['carrot', 'banana']
Item 1 to -1 is ['mango', 'carrot']
Item start to end is ['apple', 'mango', 'carrot', 'banana']
characters 1 to 3 is wa
characters 2 to end is aroop
characters 1 to -1 is waroo
characters start to end is swaroop
引用与复制
当创建一个对象并给他赋一个变量的时候,该对象引用该变量
示例:
#!/usr/bin/python
# Filename: 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
输出
$ python reference.py
Simple Assignment
shoplist is ['mango', 'carrot', 'banana']
mylist is ['mango', 'carrot', 'banana']
Copy by making a full slice
shoplist is ['mango', 'carrot', 'banana']
mylist is ['carrot', 'banana']
分析:将shoplist赋给mylist,则mylist引用shoplist,两者是同一内存空间的两个名称。
当需要保存到另一内存空间,即完成复制时,必须用 切片操作符 [ : ]
字符串更多操作
#!/usr/bin/python
# Filename: str_methods.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)
输出
$ python str_methods.py
Yes, the string starts with "Swa"
Yes, it contains the string "a"
Yes, it contains the string "war"
Brazil_*_Russia_*_India_*_China
学习:str 中 startswith(),find(),join()等方法
分享到:
评论

相关推荐

    简明 Python 教程.rar

    第9章数据结构..60 简介.......60 列表.......60 元组.......63 字典.......66 序列.......68 参考.......71 更多字符串的内容.........72 概括.......74 第10章解决问题——编写一个Python脚本....75 ...

    简明python教程电子版 pdf高清版

    简明python教程电子版 pdf高清版,Python是一种简单易学,功能强大的编程语言,它有高效率的高层数据结构,简单而有效地实现面向对象编程。Python简洁的语法和对动态输入的支持,再加上解释性语言的本质,使得它在...

    简明 Python 教程 中文版

    简明 Python 教程 中文版 无论您刚接触电脑还是一个有经验的程序员,本书都将有助您学习使用Python语言! Python是一种简单易学,功能强大的编程语言,它有高效率的高层数据结构,简单而有效地实现面向对象编程。...

    简明python教程

    9. 数据结构 简介 列表 对象与类的快速入门 使用列表 元组 使用元组 元组与打印语句 字典 使用字典 序列 使用序列 参考 对象与参考 更多字符串的内容 字符串的方法 概括 10. 解决问题——编写一个...

    数据结构python简明教程

    Python版本的数据结构教程,很简短。 zszszszszszszszszs

    简明python教程(chm)

    9. 数据结构 简介 列表 对象与类的快速入门 使用列表 元组 使用元组 元组与打印语句 字典 使用字典 序列 使用序列 参考 对象与参考 更多字符串的内容 字符串的方法 概括 10. 解决问题——...

    Python初学教程:《简明Python教程》

    Python是一种简单易学,功能强大的编程语言,它有高效率的高层数据结构,简单而有效地实现面向对象编程。Python简洁的语法和对动态输入的支持,再加上解释性语言的本质,使得它在大多数平台上的许多领域都是一个理想...

    数据结构教程李春葆课件+源码+试题

    数据结构教程李春葆课件+源码+试题,适合初学数据结构的朋友们使用

    Python-入门教程.pptx

    《简明Python教程》的描述:Python是一种简单易学,功能强大的编程语言。它有高效率的高层数据结构,能够简单、有效地实现面向对象编程。Python语法简洁,支持动态输入,是解释性语言。在大多数平台上,对于众多...

    简明 Python 教程(中文)

    Python是一种简单易学,功能强大的编程语言,它有高效率的高层数据结构,简单而有效地实现面向对象编程。Python简洁的语法和对动态输入的支持,再加上解释性语言的本质,使得它在大多数平台上的许多领域都是一个理想...

    简明Python教程

    Python的官方介绍是: Python是一种简单易学,功能强大的编程语言,它有高效率的高层数据结构,简单而有效地实现面向对象编程。Python简洁的语法和对动态输入的支持,再加上解释性语言的本质,使得它在大多数平台上...

    简明 Python 教程

    Python是一种简单易学,功能强大的编程语言,它有高效率的高层数据结构,简单而有效地实现面向对象编程。Python简洁的语法和对动态输入的支持,再加上解释性语言的本质,使得它在大多数平台上的许多领域都是一个理想...

    python基础资料+教程+简明教程+学习资料

    教程包括了Python语言的基础语法、基本数据类型、控制结构、函数、模块、面向对象编程等方面的内容,可以帮助读者初步掌握Python的编程技能。在学习本教程之前,你需要具备一些基本的编程知识。例如,对于编程的概念...

    数据结构与算法:Python语言实现课后习题答案PPT等.rar

    本资源包括该书的课后习题讲解以及书上的一些源代码,供老师上课的PPT等资源。

    python简明教程.chm

    9. 数据结构 简介 列表 对象与类的快速入门 使用列表 元组 使用元组 元组与打印语句 字典 使用字典 序列 使用序列 参考 对象与参考 更多字符串的内容 字符串的方法 概括 10. 解决问题——...

    python简明版本

    Python简明版本,为基础知识简介。帮助你在最短的时间内熟悉python的内建函数,数据结构等

    Python资料整理

    Python数据结构与算法(En) Think.Stats-Python与数据分析byAllen.B.Downey 社交网站的数据挖掘与分析 使用Python语言分析金融数据的研究 数据挖掘导论 用Python进行自然语言处理 简明Python教程 零基础学Python ...

    Python 数据挖掘资料汇总.rar

    Python数据结构与算法 Python语言入门 Think.Stats-Python与数据分析byAllen.B.Downey 集体智慧编程 如何安装Python 社交网站的数据挖掘与分析 使用Python进行自然语言处理 使用Python语言分析金融数据的研究

Global site tag (gtag.js) - Google Analytics