python中常用的内建数据结构

1、列表(list)

可以参考这里:http://www.runoob.com/python/python-lists.html

list就像C++里面的vector一样。。。如果vector用熟了,list应该也似曾相识。记住list是可变的,也就是说我们可以去直接修改它。

示例代码:

#coding=utf-8
print "this is an example of LIST!\n"
list=['apple','bread','milk','coffee']#创建列表
print "the list is:",list[0:]#从第0个元素开始截取列表
print "the first item is:",list[0]#访问列表元素
print "the last but one:",list[-2]#截取倒数第二个元素
print "let's print list[0:3):",list[0:3]#用范围来访问列表元素
print "I want a pear, add a pear to our list!\n"
list.append('pear')#添加元素
print "now show it:",list[0:]
print "now let's sort it"
list.sort()#排序
print "the result is:",list[0:]
print "delete milk!"
del list[3]#删除
print list[0:]
print "the length is:",len(list)#获取长度
print "now let's count how many apples are there in the list:",list.count("apple")#统计元素个数
print "get the index of coffee:",list.index("coffee")#获取元素下标
print "now remove coffee\n"
list.remove("coffee")#通过remove删除元素
print list[0:]
print "now reverse the list\n"
list.reverse()#反转列表
print list[0:]
运行效果:

(没有用python的ide,是在powershell下编译运行的。。虽然丑但是凑合着也能看=。=)


2、元组(tuple)

OMG。。。python所谓的“元组”长得和lisp的“列表”一模一样=。=(好吧这并没有什么意义,只是想吐槽一下=。=)

元组和列表唯一的区别在于元组是不可变的,也就是说一旦创建你就不能直接修改它,这就导致了元组没有append方法(因为不能修改,添加元素就没有意义了),也没有remove(删除某个元素)。但是tuple的删除可以用del方法,这会删去整个元组。当然,要修改元组也可以通过另一种方式:

tup1=("LiMing",18,'m')
tup2=("HanMeimei",18,'f')
list1=list(tup1)#change the tuple into a list
#now set the original value 'm' to be 'f'
list1[2]='f'
tup1=tuple(list1)
print tup1[0:]
通过list()方法和tuple()方法可以实现列表、元组之间的相互转换

3、字典(dictionary)

字典就是python中的哈希表,它的特点是:

1)字典和数学上的集合一样,是无序的

2)键值必须唯一,如果出现重复会取后一个

3)键值必须不可变(也就是说可变的东西,比如列表,不可以充当键值)

示例代码:

#coding=utf-8
dict={"name":"john","age":18,"sex":'m',"job":"salesman","location":"hangzhou"}
if(dict.has_key("salary")): #检查键值是否存在,如果存在输出yes
    print "yes"
else:
    print "no"
val=dict.get("name","no such person")#获取键值对应的值,如果不存在返回第二个参数
print "the name is: %s"%val
list=dict.keys()#以列表返回所有的键
print "the according list is:",list[0:]
items=dict.items()#返回一个列表,元素是键值对
print "the result is",items[0:]
values=dict.values()#以列表形式返回所有的值
print "the value is",values[0:]
deleted=dict.pop("age")#删除键值对,并返回这个值
print "after deleting the pair:",dict
print deleted#会打印18
'''注意:还有一种和pop类似的方法,
叫做popitem,例如:item=dict.popitem()
它会随机删除字典中的键值对,并返回这个值
'''
newdict={"salary":9000,"car brand":"volkswegen"}
dict.update(newdict)#把newdict的键值对增添到dict中
print "now the dict is",dict
keytuple=("name","age","sex")
dict2=dict.fromkeys(keytuple,"unknown")#拷贝给定的键,默认值是第二个参数
print "the new dict is",dict2
运行效果(这次用PyCharm了。。):



小结:列表、元组和字典都是“序列”,序列的基本特点是将元素和位置(也可以是“键值”)关联了起来,从而方便我们的访问。


4、集合(set)

可以用数学上学的集合来理解,也就是说它是无序的、互异的。(确定性可能不怎么体现得出来=。=)

python中的集合没有特殊的表达方式(不像列表用[],元组用(),字典用{}),而是通过函数set()来获取,例如:

list2=[1,1,1,3453,64,1231,0]
myset2=set(list2)
print "the set is",myset2
得到的结果是:

the set is set([64, 1, 3453, 0, 1231])


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值