student = {'name':'小明','age':'12','gender':'男','num':'4号'}
print(student)
输出:
{'name': '小明', 'age': '12', 'gender': '男', 'num': '4号'}
print('name:' + student['name'] + ",num:" + student['num'])
输出:
name:小明,num:4号
student['height'] = '178'
print(student)
输出:
{'name': '小明', 'age': '12', 'gender': '男', 'num': '4号', 'height': '178'}
student['age'] = '13'
print(student)
输出:
{'name': '小明', 'age': '13', 'gender': '男', 'num': '4号', 'height': '178'}
del student['num']
print(student)
输出:
{'name': '小明', 'age': '13', 'gender': '男', 'height': '178'}
people = {'avatar':'http://www.asdsd.png','nickname':'lili','gender':'M'}
print('-----遍历-----')
for k , v in people.items():
print(k + " = " + v)
print('-----遍历key-----')
for k in people.keys():
print(k)
print('-----遍历value-----')
for v in people.values():
print(v)
输出:
-----遍历-----
avatar = http://www.asdsd.png
nickname = lili
gender = M
-----遍历key-----
avatar
nickname
gender
-----遍历value-----
http://www.asdsd.png
lili
M
// 列表嵌套字典
xiaoming = {'name':'小明','age':'12'}
xiaohong = {'name':'小红','age':'11'}
xiaohu = {'name':'小虎','age':'13'}
students = [xiaoming,xiaohong,xiaohu]
for student in students:
print(student)
输出:
{'name': '小明', 'age': '12'}
{'name': '小红', 'age': '11'}
{'name': '小虎', 'age': '13'}
// 字典嵌套列表
favorites = { 'xiaoming' : ['足球','篮球'] , 'xiaohong' : ['乒乓球','羽毛球'] , 'xiaohu' : ['足球','羽毛球'] }
for k , v in favorites.items():
print(k + "爱好是:" )
for favorite in v:
print(favorite)
输出:
xiaoming爱好是:
足球
篮球
xiaohong爱好是:
乒乓球
羽毛球
xiaohu爱好是:
足球
羽毛球
// 字典嵌套字典
students = {'xiaoming' : {'name':'小明','age':'12'},'xiaohong' : {'name':'小红','age':'13'}}
for k,v in students.items():
print('拼音名:'+k)
for k1,v1 in v.items():
print(k1 + ' = ' + v1)
输出:
拼音名:xiaoming
name = 小明
age = 12
拼音名:xiaohong
name = 小红
age = 13