python字典

---恢复内容开始---

# 字典是一个无序集合,输出也是杂乱
xiaoming={"name":"小明",
          "age":18,
          "weight":75.5,
          "height":170}
print(xiaoming)

print(xiaoming["age"])

# 若key不存在,会新增键值对
xiaoming["gender"]="male"
print(xiaoming)
# 若key存在,会修改已存在的键值对
xiaoming["age"]=21
print(xiaoming)

# 迭代遍历字典
# k指的是每次循环中,获取到的键值对的key
for k in xiaoming:
    print("%s--%s" %(k,xiaoming[k]))

 

# 开发中,把多个字典放在一个列表中,再进行遍历
card_list=[
    {"name": "张三",
     "age":18,
     "phone":112},
    {"name": "李四",
     "age": 20,
      "phone": 110}
]

for info in card_list:
    print(info)

 

posted @ 2019-08-22 14:42  王小二啊  阅读(102)  评论(0编辑  收藏  举报