Python探索记(13)——字典Dictionary

简介: # @Time : 2017/7/6 20:36# @Author : 原创作者:谷哥的小弟# @Site : 博客地址:http://blog.
# @Time    : 2017/7/6 20:36
# @Author  : 原创作者:谷哥的小弟
# @Site    : 博客地址:http://blog.csdn.net/lfdfhl
# @DESC    : Dictionary

'''
字典的每个元素由两部分组成即键和值
可依据键查询其对应的值,如person['name']获取name对应的值
亦可使用get()方法,假若该键不存在则返回默认值
'''
person={'name':'谷哥的小弟','age':25,'address':'china'}
print('name=',person['name'])
print('age=',person['age'])
print('address=',person['address'])

gender=person.get('gender','男')
print('gender=',gender)
print('= '*20)

'''
利用items()即可获取字典所有元素
利用keys()获取字典中的所有key
利用values()获取字典中的所有value
利用in判断key是否在字典中;如果在则返回True,否则返回False
'''
person={'name':'谷哥的小弟','age':25,'address':'china'}
items=person.items();
for item in items:
    print('字典中的item=',item)
keys=person.keys()
for key in keys:
    print('字典中的key=',key)
values=person.values()
for value in values:
    print('字典中的value=',value)
result='gen' in person
print('result=',result)
print('= '*20)


'''
利用len()获取字典中元素个数
'''
person={'name':'谷哥的小弟','age':25,'address':'china'}
count=len(person)
print('count=',count)
print('= '*20)


'''
修改字典中的元素的值
'''
person={'name':'谷哥的小弟','age':25,'address':'china'}
person['age']=27
print('修改后的age=%i'%person['age'])
print('= '*20)

'''
往字典中添加元素

示例如下:
往字典中添加gender=male
'''
person={'name':'谷哥的小弟','age':25,'address':'china'}
person['gender']='male'
gender=person['gender']
print('gender=',gender)
print('= '*20)

'''
删除字典中的元素
'''
person={'name':'谷哥的小弟','age':25,'address':'china'}
del person['address']
items=person.items();
for item in items:
    print(item)
print('= '*20)


'''
利用clear()清空字典
'''
person={'name':'谷哥的小弟','age':25,'address':'china'}
person.clear()
count=len(person)
print('清空后字典中元素个数=',count)
print('= '*20)

结果如下:

name= 谷哥的小弟
age= 25
address= china
gender= 男
= = = = = = = = = = = = = = = = = = = = 
字典中的item= ('name', '谷哥的小弟')
字典中的item= ('age', 25)
字典中的item= ('address', 'china')
字典中的key= name
字典中的key= age
字典中的key= address
字典中的value= 谷哥的小弟
字典中的value= 25
字典中的value= china
result= False
= = = = = = = = = = = = = = = = = = = = 
count= 3
= = = = = = = = = = = = = = = = = = = = 
修改后的age=27
= = = = = = = = = = = = = = = = = = = = 
gender= male
= = = = = = = = = = = = = = = = = = = = 
('name', '谷哥的小弟')
('age', 25)
= = = = = = = = = = = = = = = = = = = = 
清空后字典中元素个数= 0
= = = = = = = = = = = = = = = = = = = = 
相关文章
|
2月前
|
存储 Python
python将字典的键或值解包到变量中
【7月更文挑战第5天】
32 4
|
2月前
|
Python
|
1月前
|
存储 索引 Python
Python学习笔记----列表、元组和字典的基础操作
这篇文章是一份Python学习笔记,涵盖了列表、元组和字典的基础操作,包括它们的创建、修改、删除、内置函数和方法等。
Python学习笔记----列表、元组和字典的基础操作
|
24天前
|
存储 Python 容器
python字典的常用操作方法
python字典的常用操作方法
|
25天前
|
存储 JSON JavaScript
使用 Python 将字典转换为 JSON
【8月更文挑战第27天】
18 2
|
1月前
|
存储 索引 Python
六:《Python基础语法汇总》— 字典和序列操作
本篇文章讲解了对字典元素的索引,以及字典常用的方法和函数;对字典的遍历;字典推导式和关于序列的运算符及方法
16 2
|
23天前
|
存储 数据库 Python
Python 中的字典是什么?
【8月更文挑战第29天】
20 0
|
23天前
|
Python
python在列表、元素、字典、集合和numpy的数组前加上星号 * 是什么含义,以及*args和**kwargs的使用
python在列表、元素、字典、集合和numpy的数组前加上星号 * 是什么含义,以及*args和**kwargs的使用
24 0
|
1月前
|
存储 索引 Python
探究 Python3 字典的现世
【8月更文挑战第6天】Python 3 中的字典是一种内置数据类型,采用键值对形式存储数据,支持通过键快速检索值。字典无序且可变,键唯一。创建字典可用 `{}` 或 `dict()` 函数,访问、更新和删除条目分别通过索引、`update()` 和 `del` 实现。
34 1
|
1月前
|
JSON 数据格式 Python
【python】解决json.dump(字典)时报错Object of type ‘float32‘ is not JSON serializable
在使用json.dump时遇到的“Object of type ‘float32’ is not JSON serializable”错误的方法,通过自定义一个JSON编码器类来处理NumPy类型的数据。
49 1