len(字典名)
举例:
person={"姓名":"张三","年龄":20,"性别":"男"}
print(len(person))
输出:
3
字典名.keys()
举例:
person={"姓名":"张三","年龄":20,"性别":"男"}
print(person.keys())
persons=person.keys()
print(type(persons))
输出:
dict_keys(['姓名', '年龄', '性别'])
<class 'dict_keys'>
字典名.values()
举例:
person={"姓名":"张三","年龄":20,"性别":"男"}
print(person.values())
persons=person.values()
print(type(persons))
输出:
dict_values(['张三', 20, '男'])
<class 'dict_values'>
字典名.items()
person={"姓名":"张三","年龄":20,"性别":"男"}
print(person.items())
persons=person.items()
print(type(persons))
输出:
dict_items([('姓名', '张三'), ('年龄', 20), ('性别', '男')])
<class 'dict_items'>
键名,键值,键名对应键值的遍历。
举例:
person={"姓名":"张三","年龄":20,"性别":"男"}
persons_1=person.keys()
persons_2=person.values()
persons_3=person.items()
for a in persons_1://键名的遍历
print(a,end=' ')
print("\n")
for b in persons_2://键值的遍历
print(b,end=' ')
print("\n")
for c in persons_3://键名与对应的键值的遍历
print(c,end=' ')
输出:
姓名 年龄 性别
张三 20 男
('姓名', '张三') ('年龄', 20) ('性别', '男')
person={"姓名":"张三","年龄":20,"性别":"男"}
for keys in person.keys()://键名的遍历
print(keys,end=' ')
print("\n")
for values in person.values()://键值的遍历
print(values,end=' ')
print("\n")
for key,values in person.items()://键名与对应的键值的遍历
print(key,values)
输出:
姓名 年龄 性别
张三 20 男
姓名 张三
年龄 20
性别 男
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
长按识别二维码并关注微信
更方便到期提醒、手机管理