码迷,mamicode.com
首页 > 编程语言 > 详细

Byte Of Python(数据结构)

时间:2017-11-02 01:03:14      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:安全   mutable   tac   bind   ngui   character   开始   函数   运算符   

数据结构

数据结构(Data Structure)基本上人如其名——它们只是一种结构,能够将一些数据聚合在一起。换句话说,它们是用来存储一系列相关数据的集合。

Python中有四种内置的数据结构——列表(List)、元组(Tuple)、字典(Dictionary)和集合(Set)。

 

列表

列表是一种用于保存一系列有序项目的集合。列表是一种可变(Mutable)数据类型。

 

 1 #This is my shopping list
 2 shoplist = [apple, mango, carrot, banana]
 3 
 4 print(I have, len(shoplist), items to purchase.)
 5 
 6 print(These items are:, end= )
 7 for item in shoplist:
 8     print(item,end= )
 9 
10 print(\nI also have to buy rice.)
11 shoplist.append(rice)
12 print(My shopping list is now, shoplist)
13 
14 print(I will sort my list now.)
15 shoplist.sort()
16 print(Sorted shopping list is, shoplist)
17 
18 print(The first item I will buy is, shoplist[0])
19 olditem = shoplist[0]
20 del shoplist[0]
21 print(I bought the, olditem)
22 print(My shopping list is now, shoplist)

 

 

元组

元组用于将多个对象保存到一起。元组的一大特征类似于字符串,它们是不可变的,也就是说,你不能编辑或更改元组。

元组通常用于保证某一语句或某一用户定义的函数可以安全地采用一组数值,即元组内的数值不会改变。

# 我会推荐你总是使用括号
# 来指名元组的开始与结束
# 尽管括号是一个可选选项
# 明了胜过晦涩,显式优于隐式
zoo = (python, elephant, penguin)
print(Number of animals in the zoo is, len(zoo))

new_zoo = monkey, camel, zoo

print(Number of cages 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])
print(Number of animals in the new zoo is, len(new_zoo)-1 + len(new_zoo[2]))

‘‘‘
一个空的元组由一对圆括号构成,例如myempty = ()
然后一个只拥有一个项目的元组,必须在第一个项目的后面加上一个逗号来指定它,如此一来Python才可以识别出来这个表达式想表达的
究竟是一个元组还是一个被括号所环绕的对象。例如singleton = (2, )
‘‘‘

 

字典

字典将键(Keys)与值(Values)联立在一起。

注意:

1. 键必须是唯一的。

2. 只能使用不可变的对象(如字符串)作为字典的键,但是你可以使用可变或不可变的对象作为字典的值。

在字典中,你可以通过使用符号构成d = {key : value1, key2 : value2}这样的形式,来成对地指定键与值。

字典中成对的键值不会以任何方式进行排序。如果你希望为它们安排一个特别的次序,只能在使用它们之前自行进行排序。

# ‘ab‘是地址(Address)簿(Book)的缩写

ab = {
    Swaroop: swaroop@swaroopch.com,
    Larry: larry@wall.org,
    Masumoto: matz@ruby-lang.com,
    Spammer: spammer@hotmail.com
}

print("Swaroopl‘s address is", ab[Swaroop])

#删除一对键值对
del ab[Spammer]

print(\nThere are {} contacts in the address-book\n.format(len(ab)))

for name, address in ab.items():
    print(Contact {} at {}.format(name,address))

#添加一对键值对
ab[Guido] = guido@python.org

if Guido in ab:
    print("\nGuido‘s address is", ab[Guido])

 

序列

列表、元组与字符串都可以看做序列(sequence)的某种表现形式。序列的主要功能是资格测试(Membership Test)(也即是in与not in表达式)和索引操作(Indexing Operations),它们能够允许我们直接获取序列中的特定项目。

序列同样拥有一种切片(Slicing)运算符。

shoplist = [apple, mango, carrot, banana]
name = swaroop

#Indexing or ‘Subscription‘ operation#
#索引或“下标(Subscription)”操作符#
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])
print(Character 0 is, name[0])

#Slicing on a list#
print(Item 1 to 3 is, shoplist[1:3])
print(Item 2 to end is, shoplist[2:])
print(Item 2 to -1 is, shoplist[2:-1])
print(Item 1 to -1 is, shoplist[1:-1])
print(Item Start to end is, shoplist[:])

#从某一字符串中切片#
print(character 1 to 3 is, name[1:3])
print(character 2 to end is, name[2:])
print(character 1 to -1 is, name[1:-1])
print(character start to end is, name[:])

 

集合

集合(Set)是简单对象的无序集合(Collection)。当集合中的项目存在与否比起次序或其出现次数更加重要时,我们就会使用集合。

bri = set([brazil, russia, india])

print(india in bri)
print(usa in bri)

bric = bri.copy()
bric.add(China)
print(bric.issuperset(bri))
print(bric)

bri.remove(russia)
print(bri & bric)

 

引用

当你创建了一个对象并将其分配给某个变量时,变量只会查阅(Refer)某个对象,并且它也不会代表本身。也就是说,变量名只是指向你计算机内存中储存了相应

对象的那一部分。这叫做名称绑定(Binding)给那个对象。

print(Simple Assignment)
shoplist = [apple, mango, carrot, banana]
#mylist只是指向同一对象的另一种名称
mylist = shoplist

#我购买了第一项项目,所以我将其从列表中删除。
del shoplist[0]

print(shoplist is, shoplist)
print(mylist is, mylist)
#注意到shoplist和mylist两者都
#打印出了其中都没有apple的同样的列表,以此我们确认
#它们指向的是同一个对象

print(Copy by making a full slice.)
#通过生成一份完整的切片制作一份列表的副本
mylist = shoplist[:]
#删除第一个项目
del mylist[0]

print(shoplist is, shoplist)
print(mylist is, mylist)
#注意到现在两份列表已经不同了

如果你希望创建一份诸如序列等复杂的副本(而非整数这种简单的对象(Object)),你必须使用切片操作来制作副本。

如果你仅仅是将一个变量名赋予给另一个名称,那么它们都将“查阅”同一个对象,如果不小心将造成麻烦。

 

 

有关字符串的更多内容

#这是一个字符串对象
‘‘‘
startswith方法用于查找字符串是否以给定的字符串内容开头
in运算符用以检查给定的字符串是否是查询的字符串中一部分
find方法用于定位字符串中给定的子字符串的位置。如果找不到相应子字符串,find会返回-1
str类拥有一个简洁的方法用以联结(Join)序列中的项目,其中字符串将会作为每一个项目之间的分隔符,并以此生成并返回一串更大的字符串。
‘‘‘
name = Swaroop if name.startswith(Swa): print(Yes, the string is 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] print(delimiter.join(mylist))

 

Byte Of Python(数据结构)

标签:安全   mutable   tac   bind   ngui   character   开始   函数   运算符   

原文地址:http://www.cnblogs.com/Mmumu/p/7769134.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!