Python 符号数据类型

Published on 2015 - 09 - 29

List 数组 符号:[ ]

List数组可被更改,例:

liebiao = ["test1","test2","test3","test4","test5"]
print liebiao[1]
print liebiao[4]
>>> test2
>>> test5

liebiao[0] = test6
print liebiao
>>> test6 test2 test3 test4 test5

tuple 元组符号:( )

元组不可以被更改,里面的元素是固定不变的,例:

yuanzu = ("test1","test2","test3","test4","test5")
print yuanzu[0]
>>> test1

yuanzu[1] = "test6"
>>> Error

集合 符号:set( )

set可求交集,例:

a = set("abcnmaaaaggsng")
b = set("cdfm")
x = a & b
print x
>>>set(['c', 'm'])

set可求并集

a = set("abcnmaaaaggsng")
b = set("cdfm")
y = a | b
print y
>>>set(['a', 'c', 'b', 'd', 'g', 'f', 'm', 'n', 's'])

set可求差集

a = set("abcnmaaaaggsng")
b = set("cdfm")
z = a - b
print z
>>>set(['a', 's', 'b', 'g', 'n'])

set去重

a = set("abcnmaaaaggsng")
print set(a)
>>>set(['a', 'c', 'b', 'g', 'm', 's', 'n'])

dict 字典 key = value 符号:{ }

dict 字典的格式是项目名:内容,例:

k = {"Name":"Rich","HomeTown":"Jilin"}
print k["Name"]
>>>Rich

添加项目

k["like"] = "music"
print k["Name"]
print k["like"]
>>>RICH
>>>music