Python——字典

简介: Python——字典

什么是字典?


       有时候我们需要存储一组相关党得数据的时候,比如要存储一个人的信息,那么有user_name, age, birthday 等,如果这些信息都存储在列表中,比如[ 'user_name' , 'age' , 'birthday'] 那么用起来可能不是很方便。比较方便的操作是,我直接通过user_name这个key就可以拿到这个值,我通过user_name就可以给这个key设置值值,那么就可以通过字典的方式实现我们的需求。


字典基础


1.创建字典:我们可以通过两种方式创建字典:


       person = { " user_name" :'python' ,'age' : 19}


或者是使用dirc函数

person =  dict(username = 'python' ,age = 19)

# 字典是个什么 key : value
# 字典如何定义
#定义一个空字典 dict
student_dict = {}
print(student_dict)
print(type(student_dict))
#
student_dict1 = {
    "user_name": "Micheal",
    "age": 18,
    "birthday": "1017"
}
print(student_dict1["age"])
student_dict2 = {
    "user_name": "Jack",
    "age": 19,
    "birthday": "1018"
}
student_list3 = [student_dict1, student_dict2]
print(student_list3)
for obj in student_list3:
    print(obj["age"])


2.基本操作


       len(d):返回字典的键值对的长度


       d[k]:获取k这个key对应的值


       d[k] = v:设置键为k的值v ,如果字典中不存在键为k的这一项,那么自动的添加进去


       k   in   d :检查d这个字典中是否包含键为k的这一项字典中的键可以是任意的不可变类型,比如:浮点类型、整形、字符串

# 字典是个什么 key : value
# 字典如何定义
#定义一个空字典 dict
student_dict = {}
print(student_dict)
print(type(student_dict))
#
student_dict1 = {
    "user_name": "Micheal",
    "age": 18,
    "birthday": "1017"
}
print(student_dict1["age"])
student_dict2 = {
    "user_name": "Jack",
    "age": 19,
    "birthday": "1018"
}
# key : value
print("*" * 50)
print(len(student_dict1))
#
student_dict1["age"] = 20
print(student_dict1)
print(student_dict1["user_name"])
if "user_name1" in student_dict1:
    print("在")
else:
    print("不在")
#
student_dict3 = dict(
    user_name="Micheal",
    age=18,
    birthday="1017",
)
print(student_dict3)
student_dict2 = {
    "user_name": "Jack",
    "age": 19,
    "birthday": "1018"
}

字典的常用方法:


1.clear:清除字典中所有的项


       a = {”user_name" : 'python' , 'age' : 19}


       print(a)


       a.clear()


       print(a)


2.get:访问字典中那个键对应的那个值,这个方法不会抛出异常


       a = { "user_name" : 'python' , 'age' : 19}


       username = a.get('username')


       print(username)


       city = a.get('city') #获取到的是一个None


       #也可以指定一个,在没有获取到这个值时候的默认值


       city = a.get('city','changsha') # 返changsha


       city = a[ 'city' ] #抛出异常




1bcfbed3d6de4ce1910b60885dc37222.png



3.pop:用来获得对应于给定键的值,然后将这个键和值的项从字典中删除。会返回这个值

       d = { 'x' : 1 , 'y' : 2}

       d.pop('x')

5e58f3589dc14fc69cb22e1e1b9826ff.png

4.update:用一个字典更新另外一个字典,如果碰到相同的键,则会覆盖。


       a = { ‘url' : 'http://www.baidu.com/' ,'title' : "baidu"}


       b = { "url" : "http://www.google.com/",'new_value' : "new_value"}


       a.update(b)


       print(a)


第一种:


e5b27f6ed9324791be62c0d079904898.png


第二种:


32ed57e45bd141e69c4623d93af1f61a.png


第三种:




3d8f67f6dfb0494c9639e48a36c34bee.png



416ff2f73bef4573b59045ca8319791d.png

第四种:

89c411ae47f644be9c258ce2d362056a.png

相关文章
|
10天前
|
存储 Python
python将字典的键或值解包到变量中
【7月更文挑战第5天】
14 4
|
10天前
|
Python
|
10天前
|
Python
python解包字典到新的字典
【7月更文挑战第5天】
12 1
|
3天前
|
存储 Python 容器
Python字典
【7月更文挑战第12天】Python字典
6 2
|
7天前
|
存储 Python
【Python】已解决:Python读取字典查询键报错“KeyError: ‘d‘”
【Python】已解决:Python读取字典查询键报错“KeyError: ‘d‘”
12 1
|
11天前
|
Python
Python中字典 直接解包
【7月更文挑战第4天】
17 3
|
10天前
|
Python
python解包字典到函数参数
【7月更文挑战第5天】
12 2
|
11天前
|
Python
Python中字典解包使用*和**操作符
【7月更文挑战第4天】
13 3
|
18天前
|
Python
Python中解包到单独的变量对于字典
【6月更文挑战第20天】
23 11
|
11天前
|
Python
Python中字典解包解包到变量
【7月更文挑战第4天】
12 1