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

相关文章
|
16小时前
|
Python
【Python操作基础】——字典,迭代器和生成器
【Python操作基础】——字典,迭代器和生成器
|
20小时前
|
索引 Python
Python中的列表、元组和字典各具特色
Python中的列表、元组和字典各具特色:列表是可变的,元组不可变,字典亦可变;列表和元组有序,字典无序(但在Python 3.7+保持插入顺序);元素类型上,列表和元组元素任意,字典需键不可变;列表用方括号[],元组用圆括号(),字典用大括号{}表示。列表不适合作字典键,元组可以。选择数据结构应依据实际需求。
7 2
|
3天前
|
开发者 Python
【Python 基础】递推式构造字典(dictionary comprehension)
【5月更文挑战第8天】【Python 基础】递推式构造字典(dictionary comprehension)
|
13天前
|
Python
Python中字典和集合(二)
Python中字典和集合(二)
|
13天前
|
存储 算法 索引
Python中字典和集合(一)
Python中字典和集合(一)
|
13天前
|
存储 缓存 Python
【Python21天学习挑战赛】字典 && 小数据池
【Python21天学习挑战赛】字典 && 小数据池
|
15天前
|
存储 JSON 数据处理
|
15天前
|
存储 缓存 人工智能
bidict,一个超酷的 Python 双向字典库!
bidict,一个超酷的 Python 双向字典库!
19 1
|
15天前
|
存储 人工智能 索引
Python中的嵌套字典访问与操作详解
Python中的嵌套字典访问与操作详解
22 1
|
16天前
|
JSON 数据可视化 定位技术
python_将包含汉字的字典数据写入json(将datav的全省数据中的贵州区域数据取出来)
python_将包含汉字的字典数据写入json(将datav的全省数据中的贵州区域数据取出来)
19 0