python数据结构有哪些_python数据结构

一:数据结构

数据结构可以认为他们是用来处理一些数据的或者说是存储数据。

对于数据结构的介绍会关系到类和对象的定义,此处对这两个定义加以描述。

何为类:说道类首先我们能够想到类型,在数据结构中类型有哪些常用的类型有int整型,float浮点型,等。在Python中类是有方法的,我们可以简单理解为对这一类可以执行哪些操作。

何为对象:对象就是实际定义的一个变量, i = 5 i的类型是int整型,对象就是i.

二: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

变量shoplist是某人的购物列表。在shoplist中,我们只存储购买的东西的名字字符串,但是记住,你可以在列表中添加 任何种类的对象 包括数甚至其他列表。

我们也使用了for..in循环在列表中各项目间递归。从现在开始,你一定已经意识到列表也是一个序列。

注意,我们在print语句的结尾使用了一个 逗号 来消除每个print语句自动打印的换行符。这样做有点难看,不过确实简单有效。

接下来,我们使用append方法在列表中添加了一个项目,就如前面已经讨论过的一样。然后我们通过打印列表的内容来检验这个项目是否确实被添加进列表了。打印列表只需简单地把列表传递给print语句,我们可以得到一个整洁的输出。

再接下来,我们使用列表的sort方法来对列表排序。需要理解的是,这个方法影响列表本身,而不是返回一个修改后的列表——这与字符串工作的方法不同。这就是我们所说的列表是 可变的 而字符串是 不可变的 。

最后,但我们完成了在市场购买一样东西的时候,我们想要把它从列表中删除。我们使用del语句来完成这个工作。这里,我们指出我们想要删除列表中的哪个项目,而del语句为我们从列表中删除它。我们指明我们想要删除列表中的第一个元素,因此我们使用del shoplist[0](记住,Python从0开始计数)

输出结果为$ 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']

四:元组

元组和列表十分类似,只不过元组和字符串一样是不可变的即你不能修改元组。元组通过圆括号中用逗号分割的项目定义。元组通常用在使语句或用户定义的函数能够安全地采用一组值的时候,即被使用的元组的值不会改变。

#!/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]

len(zoo)是获取元组的长度,说明元组是一个序列。

定义的new_zoo 是新创建一个元组,应为元组是一个不可变得,不能再原处直接改变,需要另定义一个新的元组,新的元组内有叠加了一个zoon的元组。

获取新的元组序列。

输出新的元组序列

获取新元组序列内的第3个序列,应为是从0开始计数的。

获取第三个元组内的第三个元素。

输出结果$ 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

五:字典

字典类似于有一个键值通过这个键值查找对应的信息。注意,键必须是唯一的

注意,你只能使用不可变的对象(比如字符串)来作为字典的键,但是你可以使用可变或不可变的对象作为字典的值。基本说来就是,你应该只使用简单的对象作为键。键值对在字典中以这样的方式标记:d = {key1 : value1, key2 : value2 }。注意它们的键/值对用冒号分割,而各个对用逗号分割,所有这些都包括在花括号中。

记住字典中的键/值对是没有顺序的。如果你想要一个特定的顺序,那么你应该在使用前自己对它们排序。字典是dict类的实例/对象。

#!/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']

字典为ab 输出字典ab内键值是Swaroop

添加一个键值是 Guido:guido@python.org

删除一个键值是 Sapmmer

输出字典ab的序列个数

把字典对应的改变成数组。

如果字典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

六:序列

列表、元组和字符串都是序列,但是序列是什么,它们为什么如此特别呢?序列的两个主要特点是索引操作符和切片操作符。索引操作符让我们可以从序列中抓取一个特定项目。切片操作符让我们能够获取序列的一个切片,即一部分序列。 字典不是序列。

#!/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[:]

序列切片从0 开始,-1代表着倒数 也是最后一个。

shoplist[1:3] 代表从1序列开始到3序列

shoplist[:] 代表所有序列

shoplist[:-1] 代表除了最后一个所有的序列。

六:参考

当你创建一个对象并给它赋一个变量的时候,这个变量仅仅参考那个对象,而不是表示这个对象本身!也就是说,变量名指向你计算机中存储那个对象的内存。这被称作名称到对象的绑定。

wKioL1Tu_JLDdFEqAABORQIZLi4743.jpg

变量一等于变量二。其实就是改下变量二的指针指向变量一的对象,如果变量一发生改变那么变量二也会跟着改变。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值