#列表中查找数据是根据下表索引来查找 字典中是按照键来获取值的
dic={
'name':'jackson',
'age':20,
'gender':'male',
'hobby':'dance'
}
#查找键 通过键来获取值 key / value
name=dic['name']
print('name =',name)

#通过键来赋值 键对应的值
dic['job']='idol'
#修改键值对 也即 对已经有值了的键直接进行赋值 有了新的值
dic['hobby']='sing and dance'
#展示字典中的内容键值对 通过循环items来进行查找 keys / values
for i, j in dic.items():
print(i,j)

#循环遍历字典中的键
for i in dic.keys():
print(i)
# 循环遍历字典中的值
for j in dic.values():
print(j)

#删除字典中的键 并且将删除的内容进行展示
del dic['hobby']
print(dic['hobby'])

#清空字典
print('清空前的字典内容*%s '% dic)
dic.clear()
print('清空后的字典内容*%s' % dic)

dic={
'name':'jackson',
'age':20,
'gender':'male',
'hobby':'dance'
}
#字典的长度
print(len(dic))
#字典的键keys
print(dic.keys())
#字典的值values
print(dic.values())
#字典的键值对
print(dic.items())

#判断字典的键是否在字典中
print('name' in dic)

内置方法



多维元组
#多维元组 多维列表
tu=[(2,3),(4,5),(6,7)]
lis=[
['a','b','c'],
['d','e','f'],
['g','h','i','g','k']
]
print(tu[2][1])
for liin lis:
for iin li:
print(i)

网友评论