python简明教程学习笔记 数据结构

    数据结构(Data Structure)就是用来储存一系列相关数据的集合。

    python中有四种数据结构,它们分别是列表(List)、元组(Tuple)、字典(Dictionary)、集合(Set)。通过这四种数据结构,python可以使我们的编程之路更加简单。

1.列表

    列表是一种可变的数据类型,我们可以在其中添加、删除、改变、搜索项目。

    在列表之中,我们常用的命令有以下几种,分别是

    append:在列表的结尾加入一个新的成员

    sort:对列表进行排序

    del:对列表中某一个成员进行删除操作

    你可以像这样地操作他们:

shoppingList = ['apple', 'pineapple', 'salt']
print('There are', len(shoppingList), 'items I want to buy')
print('There are', end = ' ')
for item in shoppingList:
    print(item, end = ' ')

shoppingList.append('meat')
print('\n')
print('There are', end = ' ')
for item in shoppingList:
    print(item, end = ' ')

shoppingList.sort()
print('\n')
print('There are', end = ' ')
for item in shoppingList:
    print(item, end = ' ')

olditem = shoppingList[0]
del shoppingList[0]
print('\n')
print('olditem is', olditem)
print('There are', end = ' ')
for item in shoppingList:
    print(item, end = ' ')

    在这个示例列表中,我们只向列表中加入了字符串,但是我们可以向列表中添加任何类型的对象,包括数字甚至是列表。我们可以使用 for...in... 来对列表进行循环遍历,因为列表的本质也是一种序列。

    在对列表进行排序的时候,我们会发现只有列表的顺序发生了改变,列表中的内容并没有发生变化。这也表明了列表是可变的,但是字符串却是不可变的。

2.元组

    元组(Tuple) 用于将多个对象保存到一起。可以将它们近似地看作列表,但是元组不能提供列表类能够提供给你的广泛的功能。元组的一大特征类似于字符串,它们是不可变的,也就是说,我们无法编辑或更改元组。

    元组是通过特别指定项目来定义的,在指定项目时,我们可以给它们加上括号,并在括号内部用逗号进行分隔。可以注意到,元组使用普通括号进行标识,而不是采用列表那样的方括号。

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

    在元组的使用中,元组中的对象类型也可以是字符串、数字、元组,甚至可以是列表,我们的一个小程序如下所示

shoppingList = ['apple', 'pineapple', 'salt']
zoo = ('python', 'elephant', 'penguin')
print('Number of animals in the zoo is', len(zoo))
new_zoo = ('monkey', 'camel', zoo, shoppingList)
print('Number of cages 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])
print('Number of animals in the new zoo is',
len(new_zoo)-1+len(new_zoo[2]))

3.字典

    在字典中,我们将键值(Keys)与值(Value)联系在一起。要注意的是,键值必须是唯一的,键值与键值之间不可出现重复的现象。在字典中,我们只能使用不可变的对象(如字符串)作为键值,但是字典中的值是可以改变的。

    在字典中,我们可以通过使用如下方式来成对地指定键值与值。在这里要注意到成对的键值与值之间使用冒号分隔,而每一对键值与值则使用逗号进行区分,它们全都由一对花括号括起。

d = {'key' : 'value1' , 'key2' : 'value2'}

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

    同样的,在字典中我们也可以进行修改、删除、添加等一系列操作,但是添加操作只能在字典的末尾进行添加,无法在字典的某一个地方随意的进行添加。同时,我们也可以使用 if...in... 的语句来确定字典中是否有某一个元素。

num = {'first' : 1, 'second' : 2, 'third' : 3}
for item in num:
    print('The', item, 'one is', num[item])

del num['first']
print('\n')
for item in num:
    print('The', item, 'one is', num[item])

num['forth'] = 4
print('\n')
for item in num:
    print('The', item, 'one is', num[item])

num['third'] = 30
print('\n')
for item in num:
    print('The', item, 'one is', num[item])

print('\n')
if 'second' in num:
    print(num['second'])

4.序列

    序列的主要功能为“资格测试(Membership Test)”(也就是 in 或者 not in 表达式)和“索引操作(Indexing Operations )”它们能够允许我们直接获取序列中的特定项目。

    上文提到的列表、元组、字典都可以看作序列的某种表现形式,因此,我们可以用操作序列的方法来对这些数据结构进行操作。

shoplist = ['apple', 'mango', 'carrot', 'banana']
name = 'swap'
# 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 1 to -1 is', shoplist[1:-1])
print('Item start to end is', shoplist[:])
# 从某一字符串中切片 #
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[:])

    与C不同的是,python中的索引可以为负数,当出现负数的时候,位置计数将从最后一个数开始,shoplist[-1]指的是序列的最后一个项目。

    当序列中出现使用“:”的一对数字时,序列将会输出冒号左边起始位置对应的值,但不会输出右边结束位置所对应的值。

    以一个含有四个项目的序列为例 sample = ['one', 'two', 'three', 'four'],one 的索引值可以是 sample[0] 也可以是 sample[-4],因此 sample[1 : -1] 指的即是 'two' 'three'

5.集合

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

    python中集合的操作和数学中的完全相同,并且要注意的是,集合中的 .copy() 方法所进行的是深拷贝,更改原集合不会对复制之后集合中的内容造成影响。

print('原集合')
bri = set(['China', 'USA', 'Japan'])
print('Japan' in bri)

print('添加')
bri.add('Russia')
for item in bri:
    print(item)

bri.remove('USA')
print()
print('移除')
for item in bri:
    print(item)

print()
print('深拷贝')
bric = bri.copy()
for item in bric:
    print(item)
print()
bri.add('England')
for item in bric:
    print(item)
print()
bri.add('England')
for item in bri:
    print(item)

    在使用集合或者列表的时候,如果我们仅仅使用 A = B 的赋值方式,则代表进行了一次浅拷贝,即同一块内存地址被两个变量所指代,在任意一个变量中进行修改都会改变其对应的内存块。如果想生成一个拥有自己独立内容的指代,则需要通过一个完整的复制来制作一个列表的副本。

shopList = ['apple', 'mango', 'carrot', 'banana']
myList1 = shopList
myList2 = shopList[:]

6.有关字符串的更多内容

    字符串作为一种对象,其也有自己的方法,可以做到检查字符串的一部分或者去掉字符串中的空格等等一系列的事情。

    常见的操作是起始项确定、结束项确定、是否含有某一项、某一部分是否属于此字符串和在列表中间加入一些自定义的符号、文字等等。

# 这是一个字符串对象
name = 'Swaroop'
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"')
if name.endswith('oop'):
    print('Yes, the string ends with "oop"')
delimiter = '_*_'
mylist = ['Brazil', 'Russia', 'India', 'China']
print(delimiter.join(mylist))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值