1、元祖
元祖的特性结合了字符串和列表
元祖是用圆括号括起来的,其中的元素之间用逗号(英文半角)隔开。元祖中的元素是任意类型的python对象(包括以后自定义的对象)
元祖中的元素不可更改,所以修改列表的方法,在元祖中都会失效。
使用场景:
元祖比列表操作速度快。如果定义了一个值,并且唯一要用他做的是不断的遍历他,那么请使用元祖代替列表。
如果对不需要修改的数据进行"写保护",即该数据是常量,那么此时也要使用元祖。如果必须要改变这些值,则可以转换为列表修改。
元祖可以在字典(又一种对象类型,后面要讲述)中被用作key,但是列表不可以。字典的key必须是不可变的。元祖本身就是不可改变的,而列表是可变的。
1.1定义元祖:
tu1 = tuple() tu2 = (1,) tu3 = (1,23,3,‘python‘)
>>> a = (3)
>>> type(3)
<class ‘int‘> #定义时要注意的
1.2索引和切片,与列表相同
>>> t = (1,‘23‘,[123,‘abc‘],(‘python‘,‘learn‘)) >>> t[2] [123, ‘abc‘] >>> t[1:] (‘23‘, [123, ‘abc‘], (‘python‘, ‘learn‘)) >>> t[2][0] 123 >>> t[3][1] ‘learn‘
1.3转换列表
>>> t = (1,‘23‘,[123,‘abc‘],(‘python‘,‘learn‘)) >>> tls = list(t) #转换列表 >>> tls [1, ‘23‘, [123, ‘abc‘], (‘python‘, ‘learn‘)] >>> t_tuple = tuple(tls) #转换元祖 >>> t_tuple (1, ‘23‘, [123, ‘abc‘], (‘python‘, ‘learn‘))
1.4方法
dir(tuple) [‘__add__‘, ‘__class__‘, ‘__contains__‘, ‘__delattr__‘, ‘__dir__‘, ‘__doc__‘, ‘__eq__‘, ‘__format__‘, ‘__ge__‘, ‘__getattribute__‘, ‘__getitem__‘, ‘__getnewargs__‘, ‘__gt__‘, ‘__hash__‘, ‘__init__‘, ‘__init_subclass__‘, ‘__iter__‘, ‘__le__‘, ‘__len__‘, ‘__lt__‘, ‘__mul__‘, ‘__ne__‘, ‘__new__‘, ‘__reduce__‘, ‘__reduce_ex__‘, ‘__repr__‘, ‘__rmul__‘, ‘__setattr__‘, ‘__sizeof__‘, ‘__str__‘, ‘__subclasshook__‘, ‘count‘, ‘index‘]
2、字典(dictionary)
字典中的"键"必须是不可变的数据类型:"值"可以是任意数据类型
2.1创建字典
>>> mydict1 = {} >>> mydict1 {} >>> mydict2 = {‘name‘:‘zhang‘,‘site‘:‘bokeyuan‘,‘language‘:‘python‘} >>> mydict2 {‘name‘: ‘zhang‘, ‘site‘: ‘bokeyuan‘, ‘language‘: ‘python‘} #"键/值对" 前面的叫做(key),后面的叫做值(value)
2.2增加键值对
>>> mydict2[‘name2‘] = "taj" #追加键值对,内存位置不变,它是可变的 >>> mydict2 {‘name‘: ‘zhang‘, ‘site‘: ‘bokeyuan‘, ‘language‘: ‘python‘, ‘name2‘: ‘taj‘} #方法2>>> name = (["first","Google"],["second","Yahoo"])
>>> type(name)
<class ‘tuple‘>
>>> website = dict(name)
#或
>>> ad = dict(name = ‘tajzhang‘,ahe = 22)
>>> ad
{‘name‘: ‘tajzhang‘, ‘ahe‘: 22}
#方法3
>>> website = {}.fromkeys(("third","forth"),"facebook")
>>> website
{‘third‘: ‘facebook‘, ‘forth‘: ‘facebook‘}
2.3访问字典的值
>>> mydict2 = {‘name‘:‘zhang‘,‘site‘:‘bokeyuan‘,‘language‘:‘python‘} >>> mydict2["name"] ‘zhang‘
2.4基本操作
len(d),返回字典(d)中的键值对的数量
d[key],返回字典(d)中的键(key)的值
d[key]=value,将值(value)赋给字典(d)中的键(key).
del d[key],删除字典(d)的键(key)项(将该键值对删除).
key in d,检查字典(d)中是否含有键为key的项
2.5字符串格式化输出
>>> city_code = {"suzhou":"0512","ttangshan":"0315","huangzhaou":"0571"} >>> "Suzhou is a beautiful city, its area code is {suzhou}".format(**city_code)
>>> temp = "<html><head><title>{lang}<title><body><p>My name is {name}.<p><body></head><html>" >>> my = {"name":"qiwsir","lang":"python"} >>> temp.format(**my) ‘<html><head><title>python<title><body><p>My name is qiwsir.<p><body></head><html>‘ #实际应用场景
2.6字典的方法
python重要概念:对象有类型,变量无类型
1)copy,复制,表层id()不同,如果有多层序列类型,则调用相同id
d1 = d.copy()
2)d("key").remove("value")
3)d.clean() 清空字典为空字典
4)del d 删除字典
5)get 通过键取值
>>> d = {‘lang‘:‘python‘} >>> d.get(‘lang‘) ‘python‘ >>> d.get(‘name‘) #不会报错,较为宽容
6)setdefault
>>> d {‘lang‘: ‘python‘} >>> d.setdefault("lang") #与get类似返回值 ‘python‘ >>> d.setdefault("name","tajzhang") #也可以直接添加键值 ‘tajzhang‘ >>> d {‘lang‘: ‘python‘, ‘name‘: ‘tajzhang‘} >>> d.setdefault("name","zhang2") #修改值不生效 ‘tajzhang‘ >>> d {‘lang‘: ‘python‘, ‘name‘: ‘tajzhang‘} >>> d.setdefault(‘web‘) #可添加一个空的键 >>> d {‘lang‘: ‘python‘, ‘name‘: ‘tajzhang‘, ‘web‘: None}
7)items,keys,values 三种字典扩展类型
>>> dd = {‘name‘:‘tajzhang‘,‘lang‘:‘python‘,‘web‘:‘www.google.com‘} >>> dd_kv = dd.items() >>> dd_kv dict_items([(‘name‘, ‘tajzhang‘), (‘lang‘, ‘python‘), (‘web‘, ‘www.google.com‘)]) >>> type(dd_kv) <class ‘dict_items‘> >>> dd.keys() dict_keys([‘name‘, ‘lang‘, ‘web‘]) >>> dd.values() dict_values([‘tajzhang‘, ‘python‘, ‘www.google.com‘])
8)pop,popitems
>>> dd {‘name‘: ‘tajzhang‘, ‘lang‘: ‘python‘, ‘web‘: ‘www.google.com‘} >>> dd.pop(‘name‘) #指定删除 ‘tajzhang‘ >>> dd.pop(‘name‘) #不存在则删除报错 Traceback (most recent call last): File "<stdin>", line 1, in <module> KeyError: ‘name‘
>>> dd {‘lang‘: ‘python‘, ‘web‘: ‘www.google.com‘, ‘name‘: ‘zhang2‘} >>> dd.popitem() #与listpop方法类似,不指定值随机删除(因为字典是无序的),当字典为空时会报错 (‘name‘, ‘zhang2‘) #返回一个元祖
9)update更新字典内容
>>> d1 = {"lang":‘python‘} >>> d2 = {‘song‘:‘I dreamed a dream‘} >>> d1.update(d2) #把d2内容更新到d1中,d2不变 >>> d1 {‘lang‘: ‘python‘, ‘song‘: ‘I dreamed a dream‘} >>> d2 {‘song‘: ‘I dreamed a dream‘}
>>> d2 {‘song‘: ‘I dreamed a dream‘} >>> d2.update([("name","qiwsir"),("web","itdiffer.com")]) #方法2 >>> d2 {‘song‘: ‘I dreamed a dream‘, ‘name‘: ‘qiwsir‘, ‘web‘: ‘itdiffer.com‘}
2.7引入深度copy,不管数据多少层都是真复制 不同id()内存地址
>>> x = {‘lang‘:[‘python‘,‘java‘],‘name‘:‘qiwsir‘} >>> import copy >>> z = copy.deepcopy(x) >>> z {‘lang‘: [‘python‘, ‘java‘], ‘name‘: ‘qiwsir‘} >>> id(x["lang"]) 4339599752 >>> id(z["lang"]) 4339599880
3、集合
>>> s1 = set("tajzhang") #创建集合 >>> s1 {‘h‘, ‘j‘, ‘a‘, ‘g‘, ‘z‘, ‘t‘, ‘n‘} >>> s2 = set([123,"google","face","book","facebook","book"]) #指定创建 >>> s2 {‘google‘, ‘facebook‘, 123, ‘book‘, ‘face‘} >>> type(s2) <class ‘set‘>>>> s3 = {"facebook",123} #直接创建>>> s3{123, ‘facebook‘}>>> type(s3)<class ‘set‘>