标签:value 创建 for循环 count ict rom code 字典 没有
1.tu(对象)对应的tuple(类)
元组是对列表的二次加工,元素不可被修改,不能被增加或者删除
tu = (111,"alex",(11,22),[(33,44)],True,33,44,)
一般写元组的时候,推荐在最后加入逗号(,),为了做区分
2.tuple类中提供的方法
(1)索引取值
tu = (111,"alex",(11,22),[(33,44)],True,33,44,) v = tu[0] print(v)
输出结果为:111
(2)切片取值
tu = (111,"alex",(11,22),[(33,44)],True,33,44,) v = tu[0:2] print(v)
输出结果为:(111, ‘alex‘)
(3)可以被for循环,可迭代对象
tu = (111,"alex",(11,22),[(33,44)],True,33,44,) for item in tu: print(item)
输出结果为:
111
alex
(11, 22)
[(33, 44)]
True
33
44
(4)转换
a.字符串转换为元组
s = "asdfasdf0" v = tuple(s) print(v)
输出结果为:(‘a‘, ‘s‘, ‘d‘, ‘f‘, ‘a‘, ‘s‘, ‘d‘, ‘f‘, ‘0‘)
b.列表转换为元组
li = ["asdf","asdfasdf"] v = tuple(li) print(v)
输出结果为:(‘asdf‘, ‘asdfasdf‘)
c.元组转换为列表
tu = ("asdf","asdf") v = list(tu) print(v)
输出结果为:[‘asdf‘, ‘asdf‘]
d.元组转换为字符串
tu = ("asdf","asdf") v = "_".join(tu) print(v)
输出结果为:asdf_asdf
(5)元组的一级元素不可修改/删除/增加,元祖里面嵌套其他的数据类型可以被修改/删除/增加
tu = (111,"alex",(11,22),[(33,44)],True,33,44,) tu[3][0] = 567 print(tu)
输出结果为:(111, ‘alex‘, (11, 22), [567], True, 33, 44)
(6)获取指定元素在元组中出现的次数
tu = (11,22,33,44) v=tu.count(22) print(v)
输出结果为:1
(7)获取指定元素在元组中的位置
tu = (11,22,33,44) v=tu.index(22) print(v)
输出结果为:1
3.字典结构及特点
dic = {
"k1": ‘v1‘, #键值对
"k2": ‘v2‘
}
(1)字典的value可以是任何值
(2)列表、字典不能作为字典的key;布尔值作为key时会和1即true,0即false重复
info ={ 1: ‘asdf‘, "k1": ‘asdf‘, True: "123", (11,22): 123, } print(info)
输出结果为:{1: ‘123‘, (11, 22): 123, ‘k1‘: ‘asdf‘} 1: ‘asdf‘就没有显示,因为True和1这个key重复了
(3)字典是无序的(无法通过切片的形式进行索引)
4.dict类中提供的方法
(1)索引方式找到指定元素
info = { "k1": 18, 2: True, "k3": [ 11, [], (), 22, 33, { ‘kk1‘: ‘vv1‘, ‘kk2‘: ‘vv2‘, ‘kk3‘: (11,22), } ], "k4": (11,22,33,44) }
v = info[‘k3‘][5][‘kk3‘][0]
print(v)
输出结果为:11
(2)字典支持 del 删除(注意观察结果无序)
info = { "k1": 18, 2: True, "k3": [ 11, [], (), 22, 33, { ‘kk1‘: ‘vv1‘, ‘kk2‘: ‘vv2‘, ‘kk3‘: (11,22), } ], "k4": (11,22,33,44) } del info[‘k3‘][5][‘kk1‘] print(info)
输出结果为:{‘k3‘: [11, [], (), 22, 33, {‘kk3‘: (11, 22), ‘kk2‘: ‘vv2‘}], 2: True, ‘k1‘: 18, ‘k4‘: (11, 22, 33, 44)}
(3)for循环(但是不能进行while循环,因为无序)
a.输出key
info = { "k1": 18, 2: True, "k3": [ 11, [], (), 22, 33, { ‘kk1‘: ‘vv1‘, ‘kk2‘: ‘vv2‘, ‘kk3‘: (11,22), } ], "k4": (11,22,33,44) } for item in info: print(item)
输出结果为:
k1
2
k4
k3
和命令for item in info.keys():输出结果相同
b.输出value
info = { "k1": 18, 2: True, "k3": [ 11, [], (), 22, 33, { ‘kk1‘: ‘vv1‘, ‘kk2‘: ‘vv2‘, ‘kk3‘: (11,22), } ], "k4": (11,22,33,44) } for item in info.values(): print(item)
输出结果为:
[11, [], (), 22, 33, {‘kk3‘: (11, 22), ‘kk2‘: ‘vv2‘, ‘kk1‘: ‘vv1‘}]
True
(11, 22, 33, 44)
18
c.同时输出key和value
info = { "k1": 18, 2: 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)
输出结果为:
2 True
k1 18
k4 (11, 22, 33, 44)
k3 [11, [], (), 22, 33, {‘kk2‘: ‘vv2‘, ‘kk1‘: ‘vv1‘, ‘kk3‘: (11, 22)}]
和下面命令输出结果相同
for item in info.keys(): print(item,info[item])
(4)根据序列,创建字典,并指定统一的value
v = dict.fromkeys(["k1",123,"999"],123) print(v)
输出结果为:{‘999‘: 123, ‘k1‘: 123, 123: 123}
(5)删除并获取到删除的值
dic = { "k1": ‘v1‘, "k2": ‘v2‘ } v = dic.pop(‘k1‘,90) print(dic,v)
输出结果为:{‘k2‘: ‘v2‘} v1
dic = { "k1": ‘v1‘, "k2": ‘v2‘ } k,v = dic.popitem() print(dic,k,v)
输出结果为:{‘k1‘: ‘v1‘} k2 v2
pop是根据指定key删除,popitem是随机删除
(6)设置值。若设置的key存在,不设置,并获取当前key对应的值;若设置的key存在,设置,并获取当前设置的key对应的值
dic = { "k1": ‘v1‘, "k2": ‘v2‘ } v = dic.setdefault(‘k1111‘,‘123‘) print(dic,v)
输出结果为:{‘k2‘: ‘v2‘, ‘k1‘: ‘v1‘, ‘k1111‘: ‘123‘} 123
dic = { "k1": ‘v1‘, "k2": ‘v2‘ } v = dic.setdefault(‘k1‘,‘123‘) print(dic,v)
输出结果为:{‘k2‘: ‘v2‘, ‘k1‘: ‘v1‘} v1
》》》以下为比较常用的常用的方法
(7)根据key获取值,key不存在时,可以指定默认值(None)
dic = {
"k1": ‘v1‘,
"k2": ‘v2‘
}
v = dic.get(‘k1‘,111111)
print(v)
输出结果为:v1
(8)更新
dic = { "k1": ‘v1‘, "k2": ‘v2‘ } dic.update({‘k1‘: ‘111111‘,‘k3‘: 123}) print(dic)
输出结果为:{‘k3‘: 123, ‘k2‘: ‘v2‘, ‘k1‘: ‘111111‘}
和命令dic.update(k3=123,k1=‘111111‘)输出结果相同
标签:value 创建 for循环 count ict rom code 字典 没有
原文地址:https://www.cnblogs.com/quyao/p/9572889.html