Dict字典的操作
字典的操作
1.字典新增键值对
已存在内容的字典新增
alient_0 = {"color":"green",position:10}
alient_0["x_position"]= 1
alient_0["y_position"] = 2
print(alient_0)
空字典新增
alient_0 = {}
alient_0["color"] = "green"
alient_0["position"] = 10
2. 字典修改键值对
#修改字典键-值对
alien_2 = {'color':'green','points':9}
print("alient_2的颜色是:",alien_2['color'])
alien_2['color'] = 'yellow'
print("alient_2现在的颜色是:",alien_2['color'])
3. 字典删除键值对
del方法:删除指定的键值对
pop方法:根据指定键,删除指定键值对
popitem方法:删除最有一个键值对
clear方法:清空所有的键值对
alien_3 = {'color':'green','points':5}
print("删除前",alien_3)
del alien_3['points']
print("删除后",alien_3)
4. 查询内容
alien_3 = {'color':'green','points':5}
color = alien_3['color']
遍历字典
- 遍历key,value值
user = {}
user.items
#遍历字典
user_0 = {
'username': 'efermi',
'first': 'enrico',
'last': 'fermi',
}
for key,value in user_0.items
print("\nKey:"+key)
print("\nValue:"+value)
5.遍历key值
#遍历字典中的所有键
favorite_languages = {
'username': 'efermi',
'first': 'enrico',
'last': 'fermi',
}
for name in favorite_languages.keys():
print(name.title())
6.遍历value值
#遍历字典中的所有值
favorite_languages = {
'username': 'english',
'first': 'chinese',
'last': 'French',
}
for language in favorite_languages.values():
print(language.title())
字典嵌套
列表里嵌套字典
字典里嵌套列表
#存储所点披萨的信息
pizza = {
'crust':'thick',
'toppings':['mushrooms','extra cheese'],
}
print("披萨的配料有:",pizza['toppings'])
- 字典里嵌套字典
users = {
'这里我最屌':{
"姓":"小",
"名":"明",
"住址":"山卡拉"
},
'看谁最屌':{
"姓":"小",
"名":"红",
"住址":"大都市"
},
}
for username,userinfo in users.items():
full_name = userinfo["姓"]+userinfo["名"]
location = userinfo["住址"]
print("用户名:\n"+username+"\n用户信息:\n姓名:"+full_name+" 住址:"+location)
Dict字典的操作的更多相关文章
- Python 全栈开发:dict(字典)常用方法操作、dict嵌套
数据类型的划分:可变数据类型和不可变数据类型. 不可变数据类型(可哈希):元祖.bool.int.str 可变数据类型(不可哈希):list.dict,set(集合) dict(字典): dict(字 ...
- python dict字典常用操作
字典的特性:key唯一无序 '''特性:key唯一:无序''' info = { 'stu1101': "安徽", 'stu1102': "北京", 'stu1 ...
- Python数据类型的内置函数之tuple(元组),dict(字典),set(集合)
Python数据类型内置函数 - str(字符串) - list(列表) - tuple(元组) - dict(字典) - set(收集) tuple(元组)的操作 - (count)统计元组中元素出 ...
- dict 字典的常用操作
#dict 字典的常用操作: id_db.get() #获取 id_db.update() #更新(覆盖)字典 id_db.values() #打印字典里所有的values id_db.keys() ...
- Python3中dict字典的相关操作函数
字典对象的内建函数 1. clear() 清空字典. 例: >>> a = {1:3, 2:4} >>> a.clear() >>> a {} 2 ...
- Python字典的操作与使用
字典的描述 字典是一种key-value的数据类型,使用就像我们上学用的字典,通过拼音(key)来查对应字的详细内容(value). 字典的特性 1.字典是无序的(不像列表一样有下标,它通过key来获 ...
- [Redis源码阅读]dict字典的实现
dict的用途 dict是一种用于保存键值对的抽象数据结构,在redis中使用非常广泛,比如数据库.哈希结构的底层. 当执行下面这个命令: > set msg "hello" ...
- 1.12 dict 字典表
dict 字典表属于映射分类 dict的声明 >>> #dict类型 是 {}中包含若干个键值对 >>> d = dict() >>> d = { ...
- 数据类型:list列表[]、元祖tuple()、dict字典{}
List 列表[] 可变的 lst = [1,2,3,4] #改 lst[(元素下标)] = '需要修改的' #通过下表修改 lst[下标:下标] = '需要修改的' #通过范围修改 #加 lst.a ...
随机推荐
- [GraphQL] Apollo React Mutation Component
In this lesson I refactor a React component that utilizes a higher-order component for mutations to ...
- 数学之路-python计算实战(4)-Lempel-Ziv压缩(2)
Format characters have the following meaning; the conversion between C and Python values should be o ...
- 贪吃蛇c++实现
近期没事翻了一下曾经写的程序.真是不堪入目.曾经真是什么都不懂.只是有一个程序倒是挺有意思的,大二的时候写的一个贪吃蛇游戏.尽管程序非常难看,还有非常多漏洞.但也是这个程序让我真正開始喜欢上了编程.不 ...
- int、bigint、smallint 和 tinyint范围
int.bigint.smallint 和 tinyint范围使用整数数据的精确数字数据类型.bigint从 -2^63 (-9223372036854775808) 到 2^63-1 (922337 ...
- php 在同一个表单中加入和改动
大家写站点的时候可能都会遇到这样的情况,就是写一个表单,这个表单是用来加入一篇文章的,我们屁颠屁颠的在后台接收数据,然后存入数据库.如今有个问题.当你要对该文章进行改动的时候,你是怎么处理的? 我的方 ...
- [ACM] HDU 5086 Revenge of Segment Tree(全部连续区间的和)
Revenge of Segment Tree Problem Description In computer science, a segment tree is a tree data struc ...
- 记录,javascript中对象的属性名是字符串,却可以不用引号
问题描述:今日看书,里面介绍js的对象的属性名是包括空字符串在内的所以字符串 问题来了,我们平时定义的对象如下,是没有引号""or’'的 var someone = { f ...
- 树状数组 poj2352 Stars
2019-05-20 22:52:07 加油,坚持,加油,坚持 !!! #include<iostream> #include<cstdio> #include<cstr ...
- MarkDownPad 注册码
邮箱: Soar360@live.com 授权秘钥: GBPduHjWfJU1mZqcPM3BikjYKF6xKhlKIys3i1MU2eJHqWGImDHzWdD6xhMNLGVpbP2M5SN6b ...
- html基础知识整理
html 标题 <h1>这是一级标题</h1> <h2>这是二级标题</h2> <h3>这是三级标题</h3> html注释: ...