python--字典基本操作
字典
格式 key :value
# string list dict
# 1、取数据方便
# 2、速度快,
定义一个空字典:
d = dict()
或者 d = { }
infos = {'name':'张流量','sex':'男','addr':'火星','age':180}
#查
print(infos.get('phone'))#取不到这个key的话,就是None
print(infos.get('phone',110))#如果取不到这个key的话,默认就是110
print(infos['phone'])#如果key不存在会报错
#增
infos['phone']=13611087045 #增加一个key
infos.setdefault('小金库','2000w')
infos.setdefault('name','鹏妹妹') #如果key存在的话,不会修改原来key里面的值
infos['name']='鹏妹妹1' #如果key存在的话,会修改原来key对应的value
print(infos)
# #字典是无序的
# #修改
infos['name']='鹏妹妹'
print(infos)
# 删除
infos.pop('name') #指定key来删除
print(infos)
infos.popitem() #随机删除一个key
print(infos)
del infos['phone'] #指定key来删除
print(infos)
infos.clear() #清空字典
print(infos)
# #方法
print(infos.values())#获取到字典所有的value
print(infos.keys()) #获取到字典所有的key
print(infos.items()) # 获取字典所有的k-v
字典练习
people = {
'田雨':{
'age':18,
'money':200000,
'clothes':'100套',
'hzp':'n多',
'shoes':['nike','addis','lv','chanle']
},
'张流量':{
'金库':'2000w',
'house':['三环一套','4环2套'],
'cars': {
'japan':['普拉多','兰德酷路泽'],
'usa':['林肯','凯迪拉克','福特'],
'china':['五菱宏光','qq','红旗']
}
}
}
people['张流量']['cars']['usa'].append('牧马人')
people['田雨']['shoes'].append('匡威')
print(people)
people['田雨']['money'] = people['田雨']['money']+200
people['田雨']['money'] += 200
print(people)
# #直接循环一个字典的话,那么循环的是字典的key
for p in people:
print(p)
for k,v in people.items(): #循环的时候,同时取key和value
print(k,'======》',v)
练习2
users = {
'niuhanyang':'123456',
'yangyafu':'456789',
}
#所有的账号和密码
# username
# pwd
# cpwd
# print( '123456' in users ) #字典里面用in来判断的话,只是判断key是否存在
for i in range(3):
username = input('账号:').strip()
passwd = input('密码:').strip()
cpasswd = input('密码确定:').strip()
if username=='' or passwd=='' or cpasswd=='':
print('用户名/密码不能为空')
elif username in users:
print('用户名已经被注册!')
elif passwd!=cpasswd:
print('两次输入的密码不一致')
else:
print('恭喜,注册成功!')
# users.setdefault(username,passwd)
users[username]=passwd
break
else:
print('错误次数过多')
print(users)
python--字典基本操作的更多相关文章
- python字典基本操作
字典是python中五中基本数据类型之一,虽然它的赋值稍微麻烦点,但用起来真的是很方便.它用键值对来存放数据,所谓键值对,就是一个键,对应一个值,如果后面对前面的键再次赋值,第一次的值就被覆盖掉.像是 ...
- Python 字典(Dictionary) 基本操作
Python字典是一种可变容器模型,可存储任意类型对象:如字符串.数字.元组等.它以键值对(key-value)的形式存在,因此相当于Hashmap在python中的实现. §1. 创建字典 字典由 ...
- Python字典和集合
Python字典操作与遍历: 1.http://www.cnblogs.com/rubylouvre/archive/2011/06/19/2084739.html 2.http://5iqiong. ...
- python 字典排序 关于sort()、reversed()、sorted()
一.Python的排序 1.reversed() 这个很好理解,reversed英文意思就是:adj. 颠倒的:相反的:(判决等)撤销的 print list(reversed(['dream','a ...
- python字典中的元素类型
python字典默认的是string item={"browser " : 'webdriver.irefox()', 'url' : 'http://xxx.com'} 如果这样 ...
- python字典copy()方法
python 字典的copy()方法表面看就是深copy啊,明显独立 d = {'a':1, 'b':2} c = d.copy() print('d=%s c=%s' % (d, c)) Code1 ...
- python 字典实现类似c的switch case
#python 字典实现类似c的switch def print_hi(): print('hi') def print_hello(): print('hello') def print_goodb ...
- python字典的常用操作方法
Python字典是另一种可变容器模型(无序),且可存储任意类型对象,如字符串.数字.元组等其他容器模型.本文章主要介绍Python中字典(Dict)的详解操作方法,包含创建.访问.删除.其它操作等,需 ...
- Python 字典(Dictionary)操作详解
Python 字典(Dictionary)的详细操作方法. Python字典是另一种可变容器模型,且可存储任意类型对象,如字符串.数字.元组等其他容器模型. 一.创建字典 字典由键和对应值成对组成.字 ...
- Python 字典(Dictionary) get()方法
描述 Python 字典(Dictionary) get() 函数返回指定键的值,如果值不在字典中返回默认值. 语法 get()方法语法: dict.get(key, default=None) 参数 ...
随机推荐
- async,await与task.wait()或task.Result的区别
你是否曾经与我一样不理解async,await与task.wait()或者task.Result的区别? 接下来,一个Demo让你看出他们之间的区别. static void Main(string[ ...
- thinkphp if便签的使用
<foreach name="list" item='v'> <tr> <td><img class="user" s ...
- 【Python可视化】使用Pyecharts进行奥运会可视化分析~
项目全部代码 & 数据集都可以访问我的KLab --[Pyecharts]奥运会数据集可视化分析-获取,点击Fork即可- 受疫情影响,2020东京奥运会将延期至2021年举行: 虽然延期,但 ...
- 动静结合?Ruby 和 Java 的基础语法比较(入门篇)
前言 这篇文章示例代码比较多, Java 程序员可以看到一些 Ruby 相关语法和使用,Ruby 程序员可以看看 Java 的基本语法和使用方法,本文比较长,将近万字左右,预计需要十几分钟,如果有耐心 ...
- 2019-2020-1 20199308《Linux内核原理与分析》第三周作业
<Linux内核分析> 第二章 操作系统是如何工作的 2.1 函数调用堆栈 3个关键性的方法机制(3个法宝) 存储程序计算机 函数调用堆栈机制 中断 堆栈相关的寄存器 ESP:堆栈指针(s ...
- RedHat 的 crontab
Chapter 39. Automated Tasks In Linux, tasks can be configured to run automatically within a specifie ...
- 将Spring Boot应用程序注册成为系统服务
文章目录 前期准备 打包成可执行jar包 注册成为liunx服务 System V Init Systemd Upstart 在Windows中安装 Windows Service Wrapper J ...
- Floyd-Warshall算法正确性证明
以下所有讨论,都是基于有向无负权回路的图上的.因为这一性质,任何最短路径都不会含有环,所以也不讨论路径中包含环的情形!并且为避免混淆,将"最短路径"称为权值最小的路径,将路径经过的 ...
- 运行node 报错 throw er; // Unhandled 'error' event
错误提示 此端口已被占用,改换其他端口
- Ansible 配置文件详解
# config file for ansible -- http://ansible.com/ # ============================================== # ...