排行
数据库百科
核心案例
行业报告
月度解读
大事记
产业图谱
中国数据库
向量数据库
时序数据库
实时数据库
搜索引擎
空间数据库
图数据库
数据仓库
大调查
2021年报告
2022年报告
年度数据库
2020年openGauss
2021年TiDB
2022年PolarDB
2023年OceanBase
首页
资讯
活动
大会
学习
课程中心
推荐优质内容、热门课程
学习路径
预设学习计划、达成学习目标
知识图谱
综合了解技术体系知识点
课程库
快速筛选、搜索相关课程
视频学习
专业视频分享技术知识
电子文档
快速搜索阅览技术文档
文档
问答
服务
智能助手小墨
关于数据库相关的问题,您都可以问我
数据库巡检平台
脚本采集百余项,在线智能分析总结
SQLRUN
在线数据库即时SQL运行平台
数据库实训平台
实操环境、开箱即用、一键连接
数据库管理服务
汇聚顶级数据库专家,具备多数据库运维能力
数据库百科
核心案例
行业报告
月度解读
大事记
产业图谱
我的订单
登录后可立即获得以下权益
免费培训课程
收藏优质文章
疑难问题解答
下载专业文档
签到免费抽奖
提升成长等级
立即登录
登录
注册
登录
注册
首页
资讯
活动
大会
课程
文档
排行
问答
我的订单
首页
专家团队
智能助手
在线工具
SQLRUN
在线数据库即时SQL运行平台
数据库在线实训平台
实操环境、开箱即用、一键连接
AWR分析
上传AWR报告,查看分析结果
SQL格式化
快速格式化绝大多数SQL语句
SQL审核
审核编写规范,提升执行效率
PLSQL解密
解密超4000字符的PL/SQL语句
OraC函数
查询Oracle C 函数的详细描述
智能助手小墨
关于数据库相关的问题,您都可以问我
精选案例
新闻资讯
云市场
登录后可立即获得以下权益
免费培训课程
收藏优质文章
疑难问题解答
下载专业文档
签到免费抽奖
提升成长等级
立即登录
登录
注册
登录
注册
首页
专家团队
智能助手
精选案例
新闻资讯
云市场
微信扫码
复制链接
新浪微博
分享数说
采集到收藏夹
分享到数说
首页
/
【PCAP考试】Tuples、lists、dictionaries异同
【PCAP考试】Tuples、lists、dictionaries异同
学之初 学之时
2021-04-20
199
课程(免费)来源:
PCAP - Programming Essentials In Python
English 0321b
撰文/Iris帆
排版/Iris帆
全文/1440字
序言
本期分享的重点围绕tuples、lists和dictionaries的异同展开,且所有解释都在代码内部。
## index使用区别
dictionary = {"cat": "chat", "dog": "chien", "horse": "cheval"}
phone_numbers = {'boss' : 5551234567, 'Suzy' : 22657854310}
empty_dictionary = { }
print(dictionary["cat"])
#输出:chat
print(dictionary.get('cat'))
#输出:chat
print(phone_numbers['Suzy'])
#输出:22657854310
print(phone_numbers.get('Suzy'))
#输出:22657854310
#用key来输出value,在这里'cat'和'Suzy'为key。也可用
dict.get()
得到相同结果
print(dictionary[0])
#
KeyError
print(phone_numbers[1])
#
KeyError
lst = ["apple","banana","orange"]
tup = (1,('banana',"grape"),"orange")
print(lst[0])
#输出:apple
print(tup[1])
#输出:('banana', 'grape')
##
sorted()
dictionary = {"cat": "chat", "dog": "chien", "horse": "cheval"}
lst = [2,6,3,0,9,1]
tup =(2,6,3,0,9,1)
print(sorted(dictionary))
#输出仅包括dict的key,未包含value
print(sorted(lst))
print(sorted(tup))
#输出:
#['cat', 'dog', 'horse']
#[0, 1, 2, 3, 6, 9]
#[0, 1, 2, 3, 6, 9]
## tuples形式读取dictionaries的信息
dictionary = {"cat": "chat", "dog": "chien", "horse": "cheval"}
for key in dictionary.keys():
print(key, "->", dictionary[key])
dictionary = {"cat": "chat", "dog": "chien", "horse": "cheval"}
for english, french in dictionary.items():
print(english, "->", french)
#两种方式的输出均为:
#cat -> chat
#dog -> chien
#horse -> cheval
dictionary = {"cat": "chat", "dog": "chien", "horse": "cheval"}
for french in dictionary.values():
print(french)
#输出:
#chat
#chien
#cheval
dictionary = {"cat": "chat", "dog": "chien", "horse": "cheval"}
for english in dictionary.keys():
print(english)
#输出:
#cat
#dog
#horse
### Dictionaries内部处理:
以pol_eng_dictionary为例
pol_eng_dictionary = {
"zamek": "castle",
"woda": "water",
"gleba": "soil"
}
#上面的dictionary写法更简洁明了
## 修改diction内某个key的value
pol_eng_dictionary["zamek"] = "lock"
item = pol_eng_dictionary["zamek"]
print(item)
#输出: lock
## 删除dict内某个key(删除key同时也会删除value)
del pol_eng_dictionary['woda']
print(pol_eng_dictionary)
#输出:{'zamek': 'castle', 'gleba': 'soil'}
## 向dict添加新的key-value pair:
dict.update()
## 删除dictionary最后一个key-value元素:
dict.popitem()
pol_eng_dictionary = {"kwiat": "flower"}
pol_eng_dictionary.update({"gleba": "soil"})
print(pol_eng_dictionary)
#输出: {'kwiat': 'flower', 'gleba': 'soil'}
pol_eng_dictionary.popitem()
print(pol_eng_dictionary)
#输出: {'kwiat': 'flower'}
## del用于删除整个dictionary
##
dict.clear()
用来去除dictionary内所有元素
print(len(pol_eng_dictionary))
#输出: 3
del pol_eng_dictionary["zamek"]
#remove an item
print(len(pol_eng_dictionary))
#输出: 2
pol_eng_dictionary.clear()
#removes all the items
print(len(pol_eng_dictionary))
#输出: 0
print(pol_eng_dictionary)
#输出:{ }
del pol_eng_dictionary
#removes the dictionary
print(pol_eng_dictionary)
#输出:
Error
##
dict.copy()
用于复制一个dictionar
y
copy_dictionary = pol_eng_dictionary.copy()
print(copy_dictionary)
#输出:{'zamek': 'castle', 'woda': 'water', 'gleba': 'soil'}
###Tuples
##
tuple.count()
用于计算某个元素在tuples里一共多少个
tup = 3, 2, 3, 2, 4, 5, 6, 2, 7, 2, 8, 9
print(tup.count(2))
#输出: 4
print(tup.count(3))
#输出: 2
print(tup.count(4))
#输出: 1
## Tuples一些规则
tuple_1 = (1, 2, 4, 8)
print(tuple_1)
tuple_2 = 1, 2, 4, 8
print(tuple_2)
tuple_3 = (1,)
print(tuple_3)
tuple_4 = (1)
print(tuple_4)
#输出:
#(1, 2, 4, 8)
#(1, 2, 4, 8)
#(1,)
#1
t1 = [1, ]
t2 = [1]
print(t1)
print(t2)
#输出:
[1]
[1]
var = 123
t1 = (1, )
t2 = (2, )
t3 = (3, var)
t1, t2, t3 = t2, t3, t1
print(t1, t2, t3)
#输出
(2,) (3, 123) (1,)
var = 123
t1 = [1, ]
t2 = [2, ]
t3 = [3, var]
t1, t2, t3 = t2, t3, t1
print(t1, t2, t3)
#输出
[2] [3, 123] [1]
今天的分享就到这里了,感谢学习路上的你我TA!
扫码关注我吧
学之初 学之时
我努力前行,想在停下来的时候,把人生分享给您
考试认证
文章转载自
学之初 学之时
,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
评论
领墨值
有奖问卷
意见反馈
客服小墨