Python学习【第4篇】:元组魔法

template = "i am {name},age:{age}"
v = template.format(**{"name":'xiaoxing',"age":19})
print(v)
View Code

 

tu = (111,"xiaoxing",(11,22),[(33,44)],45,)
#1.书写格式
#一般写元组的时候推荐在最后加入逗号,
#元组中的一级元素不可被修改,不能增加或者删除
print(tu)

#2.索引取值
v1 = tu[0]

#3.切片取值
v2 = tu[0:3]
print(v1)
print(v2)

三个运行结果:

(111, 'xiaoxing', (11, 22), [(33, 44)], 45)
111
(111, 'xiaoxing', (11, 22))

#4.可被for 循环,可迭代对象
for item in tu:
print(item)
运行结果:

111
xiaoxing
(11, 22)
[(33, 44)]
45

 

 

 

#5.字符串,列表,元组相互之间转换
s = "xiaoxingasdf"
li = ["asdf","qwer"]
tu = ("xiao","xing")
#(1).字符串转元组
v1 = tuple(s)
print(v1)
运行结果:
('x', 'i', 'a', 'o', 'x', 'i', 'n', 'g', 'a', 's', 'd', 'f')

#(2).列表转元组
v2 = tuple(li)
print(v2)
运行结果:
('asdf', 'qwer')

#(3).元组转列表
v3 = list(tu)
print(v3)
运行结果:
['xiao', 'xing']


tu = (111,"xiaoxing",(11,22),[(33,44)],45,)
#元组,有序,元组一级元素不可被修改
v = tu[3][0][0]
print(v)
运行结果33

tu[3][0] = 567
#这里能够被修改的原因是tu[3][0],取到的值是[(33,44)]一个列表,因此列表可被修改
print(tu)
运行结果中[(33,44)]被替换成了[567]
(111, 'xiaoxing', (11, 22), [567], 45)

tu[3][0][0] = 567
#这里如果这样书写会报错,原因是tu[3][0][0]取到的是(33,44)元组中的元素,因元组不可被修改,因此会报错
tu = (11,"xiaoxing",(11,22),[(22,44)],11,)
#统计在元组中出现的次数
v1 = tu.count(11)
print(v1)
运行结果:
2

v2 = tu.index(11)
#查看索引值
print(v2)
运行结果:
0

##############################字典###########################
#1.基本结构
#info = {
      "k1":"v1",#键值对
      "k2":"v2"
}
#2.列表,字典不能作为字典的key,字典中的vaule可以是任意值
#布尔值是可以作为key值的,但是key值如果存在重复,只保留一个
info = {
1:'asdf',
"k1":"asdf",
True:"123",
(11,22):123,

}
print(info)
运行结果:
{1: '123', 'k1': 'asdf', (11, 22): 123}

#4.字典是无序的,可以通过多次运行字典,看到输出的顺序不一致

#5.字典的索引取值
info = {
"K1":18,
"K2":True,
"K3":[
11,
[],
(),
22,
33,
{
'KK1':'VV1',
'KK2':'VV2',
'KK3':(11,22),
}
],
'K4':(11,22,33,44)
}
v = info["K1"]
print(v)
运行结果:
18

v1 = info["K3"][5]['KK3'][0]
#在这里取的是11
print(v1)
运行结果:
11

#6.删除字典中的元素
info = {
"K1":18,
"K2":True,
"K3":[
11,
[],
(),
22,
33,
{
'KK1':'VV1',
'KK2':'VV2',
'KK3':(11,22),
}
],
'K4':(11,22,33,44)
}
del info["K1"]
print(info)
运行结果:
{'K2': True, 'K3': [11, [], (), 22, 33, {'KK1': 'VV1', 'KK2': 'VV2', 'KK3': (11, 22)}], 'K4': (11, 22, 33, 44)}

#删除KK1
del info["K3"][5]["KK1"]
print(info)
运行结果:
{'K1': 18, 'K2': True, 'K3': [11, [], (), 22, 33, {'KK2': 'VV2', 'KK3': (11, 22)}], 'K4': (11, 22, 33, 44)}
#获取key
for item in info.keys():
print(item)
运行结果:

K1
K2
K3
K4

#7.获取vaule

for item1 in info.values():
print(item1)
运行结果:

18
True
[11, [], (), 22, 33, {'KK1': 'VV1', 'KK2': 'VV2', 'KK3': (11, 22)}]
(11, 22, 33, 44)

 

#8.同时获取key和vaule

for item in info.keys():
print(item,info[item])
运行结果:

K1 18
K2 True
K3 [11, [], (), 22, 33, {'KK1': 'VV1', 'KK2': 'VV2', 'KK3': (11, 22)}]
K4 (11, 22, 33, 44)

 

#还有种方法

for k,v  in info.items():
print(k,v)
运行结果和上面一致




#布尔值是可以作为key值的,但是key值如果存在重复,只保留一个
info = {
"K1":18,
"K1":True,
"K3":[
11,
[],
(),
22,
33,
{
'KK1':'VV1',
'KK2':'VV2',
'KK3':(11,22),
}
],
'K4':(11,22,33,44)
}
运行结果:
{'K1': True, 'K3': [11, [], (), 22, 33, {'KK1': 'VV1', 'KK2': 'VV2', 'KK3': (11, 22)}], 'K4': (11, 22, 33, 44)}

v = dict.fromkeys(["k1",123,"999"],123)
#1.根据序列,创建字典,并指定统一的值
print(v)
运行结果:
{'k1': 123, 123: 123, '999': 123}

#2.根据key值获取vaule,若不存在时可以指定默认值,不指定时返回none
dic = {
'k1':'v1',
}
v = dic.get('k1',111)
print(v)
运行结果:
v1
dic = {
'k1':'v1',
'k2':'v2'
}
v = dic.pop('k1')
#3.删除并获取值

v1 = dic.popitem()
#随机删除
print(v1)

print(dic,v)
运行结果:

{'k2': 'v2'} v1

('k2', 'v2')

#4.设置值,若已存在,不设置,获取当前key对应的值
#不存在,设置,并获取当前key对应的值
v1 = dic.setdefault('k1','1234')
v2 = dic.setdefault('kk1',"1234")
print(v1,dic)
print(v2,dic)
运行结果:

v1 {'k1': 'v1', 'k2': 'v2', 'kk1': '1234'}
1234 {'k1': 'v1', 'k2': 'v2', 'kk1': '1234'}

 

#5.update调用的两种方式
dic.update({"k1":'520','k3':'521'})
print(dic)
运行结果:
{'k1': '520', 'k2': 'v2', 'k3': '521'}

dic.update(k1=123,k3=234,k4=456)
print(dic)
运行结果:

{'k1': 123, 'k2': 'v2', 'k3': 234, 'k4': 456}

 

字典中特别重要的有6. keys()  7.vaules()  8.items()  get   update

 

看到了day12 第10

 

 ############################整理###############################

一.数字

#int (...)

二.字符串

#replace/find/join/strip/startwith/split/upper/lower/format

template = "i am {name},age:{age}"
v = template.format(**{"name":'xiaoxing',"age":19})
print(v)
运行结果:
i am xiaoxing,age:19

 

 

三.列表

#append、extend、insert
#索引、切片、循环

四、元组
#忽略
#索引、切片、循环 以及元素不能被修改

五.字典
#get/update/keys/vaules/items
#for,索引
dic = {
'k1':'v1',
'k2':'v2'
}
v = "k1" in dic
print(v)

v2 = "v1" in dic.values()
print(v2)

六.布尔值
#0 1
#bool(......)
#None,"",(),[],{},0==>False
如:
bool(None)
运行结果为False





posted on 2018-05-19 16:54  小星1995  阅读(191)  评论(0编辑  收藏  举报

导航