# -*- mode: python; coding: utf-8 -*-
import copy
############# 元组 () ##################
tuple_name = ("apple0", "apple1", "apple2") #只定义一个元素末尾也要加分号 ,
print(tuple_name, len(tuple_name))
print(tuple_name[1])
print(tuple_name[-1]) #-->apple2 -1反向索引
sub_tuple = tuple_name[0:2] #--> apple 0 1
print(sub_tuple)
#历遍元组
for i in range(len(tuple_name)):
print("%d : %s" %(i, tuple_name[i]))
#解包
str1,str2,str3 = ("str1x", "str2x", "str3x")
print(str1, str2, str3)
############# 列表 [] ##################
listx = ["applex", "appley", "applez"]
print(listx[1])
listx.append("applei")
listx.insert(1, "applej")
listx.remove("applez")
print(listx)
listx.pop() #去掉最后一个
listx.pop(0) #去掉第一个
listx.reverse()
print(listx)
listy = listx[0:2]
print(listy)
listy.extend(listx)
print(listy)
############# 字典 {} ##################
dictx = {"a":"apple", "b":"banana", "c":"grape"}
dictx["d"] = "apple_d" #添加元素,直接赋值,无则添加,有则修改
print(dictx["b"])
print("%s, %(a)s, %(b)s" %dictx)
del(dictx["b"]) #删除字典元素
dictx.pop("d", "have no") #pop删除
print(dictx)
#历遍字典1
for k in dictx:
print ("dictx[%s]=%s" %(k,dictx[k]))
#历遍字典2
for k,v in dictx.iteritems():
print("dictx[%s]=%s" %(k,v))
#混合字典
dicty = {"a":("xxx","yyy"), "b":["zzz","iii"], "c":{"cc":"dd"}}
print(dicty["a"][1])
print(dicty["b"][1])
print(dicty["c"]["cc"])
#update 合并字典
dicty.update(dictx)
print(dicty)
#深浅拷贝
dict1 = {"a":"aaaa", "b":{"g":"grape", "o":"orange"}}
dict2 = copy.deepcopy(dict1) #深拷贝
dict3 = copy.copy(dict1)
dict2["b"]["g"] = "xxxxx"; #深拷贝不受影响
print(dict1)
dict3["b"]["g"] = "xxxxx"; #浅拷贝 会 改变dict1的值
print(dict1)
dict1["b"]["g"] = "xxyyy"; #浅拷贝 两者之间相互影响
print(dict3)