python的元祖,集合,字典的常见函数
- 2020 年 1 月 19 日
- 筆記
# 关于元祖的函数
- 以下代码
- 以下函数,对 list 基本适用
关于元祖的函数¶
- 以下代码
In [2]:
# len :获取元祖的长度
t = (1,2,3,4,5,6)
len(t)
Out[2]:
6
. . .
In [3]:
# 如果,列表或元祖中有多个最大值和多个最小值,则实际打印出哪个??
# max min :最大值最小值
# 如果,列表或元祖中有多个最大值和多个最小值,则实际打印出哪个??
print(max(t))
print(min(t))
6 1
. . .
In [5]:
t
xxxxxxxxxx
# tuple 转化成或创建元祖
l = [1,2,3,4,5,6]
t = tuple(l)
print(t)
t = tuple()
print(t)
(1, 2, 3, 4, 5, 6) ()
. . .
# 元祖的函数
- 基本跟 list 通用
Type Markdown and LaTeX: α2α2
In [8]:
最前面的
# count :计算制定数据出现的次数
t = (1,2,3,4,5,6,55,3,55,3)
print(t)
# index : 求制定元素在元祖中的索引位置
print(t.index(55))
# 如果需要查找的数字是多个,则返回最前面的一个
print(t.index(3))
(1, 2, 3, 4, 5, 6, 55, 3, 55, 3) 6 2
. . .
In [12]:
* 20
xxxxxxxxxx
# 元祖变量交换法
a = 1
b = 3
print(a)
print(b)
print("*" * 20)
# java程序员会这样写
c = a
a = b
b = c
print(a)
print(b)
print("*" * 20)
# python 写法
a,b = b,a
print(a)
print(b)
1 3 ******************** 3 1 ******************** 1 3
. . .
xxxxxxxxxx
# 集合- set
- 集合是高中数学的一个概念
- 一堆确定的无序的唯一的数据,集合中每一个数据成为一个元素
Type Markdown and LaTeX: α2α2
In [16]:
s
xxxxxxxxxx
# 集合定义
s = set()
print(type(s))
print(s)
# 此时大括号里一定要有值,否则定义出的是 dict
s = {1,2,3,4,5,6}
print(s)
<class 'set'> set() {1, 2, 3, 4, 5, 6}
. . .
In [14]:
# 如果只是用大括号定义,则定义的是一个 dict 类型
d = {}
print(type(d))
print(d)
<class 'dict'> {}
. . .
# 集合的特征
- 集合是无序的,即无法使用索引分片
- 集合内数据元素具有唯一性,可以用来排除重复数据
- 集合内的数据, str int float tuple 冰冻集合等,即内部只能放置可哈希数据
Type Markdown and LaTeX: α2α2
# 集合序列操作
Type Markdown and LaTeX: α2α2
In [17]:
"woshishui","wozaina","wozaizuoshenm"
x
# 成员检测
# in not in
s = {4,5,"woshishui","wozaina","wozaizuoshenm"}
print(s)
if "woshishui" in s:
print("大大")
if "woshini" not in s:
print("你是谁")
{4, 5, 'wozaizuoshenm', 'woshishui', 'wozaina'} 大大 你是谁
. . .
便利操作
xxxxxxxxxx
# 集合便利操作
Type Markdown and LaTeX: α2α2
In [18]:
xxxxxxxxxx
# for 循环
s = {4,5,"woshishui","wozaina","wozaizuoshenm"}
for i in s:
print(i,end=" ")
4 5 wozaizuoshenm woshishui wozaina
. . .
In [20]:
# 带有元组的集合遍历
s = {(4,5,6),("woshishui","wozaina","wozaizuoshenm"),(5,6,7)}
for k,n,m in s:
print(k,"--",n,"--",m)
5 -- 6 -- 7 4 -- 5 -- 6 woshishui -- wozaina -- wozaizuoshenm
. . .
# 集合的内置函数
xxxxxxxxxx
# 集合的内置函数
Type Markdown and LaTeX: α2α2
In [22]:
ss
xxxxxxxxxx
# 普通集合
# 以下集合在初始化后自动过滤掉重复元素
s = {2,22,333,55,66,33,11,559,66,1,2,3,45,6,12,3,2}
print(s)
# 普通的集合内置函数
ss = {i for i in s}
print(ss)
{33, 2, 66, 1, 3, 6, 11, 12, 333, 45, 559, 22, 55} {33, 2, 3, 66, 1, 6, 11, 12, 333, 45, 559, 22, 55}
. . .
In [23]:
sss
xxxxxxxxxx
# 带条件的集合内置函数
sss = {i for i in s if i % 2 == 0 }
print(sss)
{2, 66, 6, 12, 22}
. . .
In [29]:
if n ==4
# 多循环的集合内置函数
s1 = {1,2,3,4,5}
s2 = {"W","shishui","N"}
s = {m*n for m in s2 for n in s1}
print(s)
s = {m*n for m in s2 for n in s1 if n ==4}
print(s)
{'NNN', 'shishuishishuishishui', 'NNNNN', 'WW', 'NNNN', 'N', 'W', 'WWWW', 'shishui', 'shishuishishui', 'shishuishishuishishuishishuishishui', 'WWWWW', 'WWW', 'NN', 'shishuishishuishishuishishui'} {'shishuishishuishishuishishui', 'WWWW', 'NNNN'}
. . .
# 集合函数/关于集合的函数
Type Markdown and LaTeX: α2α2
In [30]:
# 集合内元素的最小值
# len, max, min, :跟其他基本函数一致
s = {589,654,321,123,258}
print(len(s))# 集合长度
print(max(s))# 集合内元素的最大值
print(min(s))# 集合内元素的最小值
5 654 123
. . .
In [32]:
l = [1,2,3] s = set(l) print(s)
x
# set :生成一个集合
l = [1,2,3]
s = set(l)
print(s)
{1, 2, 3}
. . .
In [33]:
,生成新的集合
# add : 向集合内添加元素,生成新的集合
s = {2,3,4}
s.add(546)
print(s)
{2, 3, 4, 546}
. . .
In [34]:
s
xxxxxxxxxx
l = [1,2,3]
print(s)
print(id(s))
s = set(l)
print(s)
print(id(s))
{2, 3, 4, 546} 2286297091432 {1, 2, 3} 2286297091656
. . .
In [36]:
成新的集合
# clear:原集合序列清空,不生成新的集合
l = [1,2,3]
print(s)
print(id(s))
s.clear()
print(s)
print(id(s))
set() 2286297091656 set() 2286297091656
. . .
In [37]:
# copy : 拷贝
# remove: 移除指定的值,直接改变原有值(不生成新的集合序列),如果要删除的值不错在,则报错
# discard :移除集合内指定的值,跟 remove 一样,但是如果要删除的数据不存在话,不报错
s = {1,2,3,4,5,6}
s.remove(5)
print(s)
s.discard(1)
print(s)
print("*" * 20)
s.discard(1100)
print(t)
s.remove(1100)
print(s)
# 为什么 remove 删除不存在的值会报 KeyError
{1, 2, 3, 4, 6} {2, 3, 4, 6} ******************** (1, 2, 3, 4, 5, 6, 55, 3, 55, 3)
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) <ipython-input-37-dfd60878c871> in <module>() 12 print(t) 13 ---> 14 s.remove(1100) 15 print(s) KeyError: 1100
. . .
In [39]:
# pop 随机移除一个元素
s = {1,2,3,4,5,6}
d = s.pop()
print(d)
print(s)
1 {2, 3, 4, 5, 6}
. . .
In [40]:
# 集合函数
# intersection:交集
# difference:差集
# union:并集
# issubset:检测一个元素是否为另一个元素的子集
# issuperset:检查一个元素是否为另一个元素的超集
s1 = {1,2,3,4,5,6,7}
s2 = {9,10,11,12,15,16}
s_1 = s1.intersection(s2)
print(s_1)
s_2 = s1.difference(s2)
print(s_2)
s_3 = s1.issubset(s2)
print(s_3)
set() {1, 2, 3, 4, 5, 6, 7} False
. . .
In [44]:
# 集合数学操作
s1 = {}
s2 = {}
s_1 = s1 - s2
print(s_1)
s_2 = s1 + s2
print(s_2)
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-44-758adeea170a> in <module>() 3 s2 = {} 4 ----> 5 s_1 = s1 - s2 6 print(s_1) 7 TypeError: unsupported operand type(s) for -: 'dict' and 'dict'
. . .
# frozenset :冰冻集合
- 冰冻集合就是不可进行任何修改的集合
- frozenset 是一种特殊集合
frozenset :冰冻集合¶
- 冰冻集合就是不可进行任何修改的集合
- frozenset 是一种特殊集合
In [45]:
# 创建
s = frozenset()
print(type(s))
print(s)
<class 'frozenset'> frozenset()
. . .
# dict 字典
- 字典是一种组合数据,没有顺序的组合数据,建议键值对形式出现
dict 字典¶
- 字典是一种组合数据,没有顺序的组合数据,建议键值对形式出现
In [56]:
# 字典的创建
# 创建空字典
d = {}
print(d)
# 创建空字典2
d = dict()
print(d)
# 创建有值的字典,每一组数据用冒号隔开,每一对键值用逗号隔开
d = {"one":1, "tow":2,"whree":3}
print(d)
# 用 dict 创建有内容的字典1
d = dict({"one":1, "tow":2,"whree":3})
print(d)
# 用 dict 创建有内容的字典2
# 利用关键字参数
d = dict(one=1, tow=2,whree=3)
print(d)
d = dict ([("tow",2),("three",3),("five",5)])
print(d)
{} {} {'one': 1, 'tow': 2, 'whree': 3} {'one': 1, 'tow': 2, 'whree': 3} {'one': 1, 'tow': 2, 'whree': 3} {'tow': 2, 'three': 3, 'five': 5}
. . .
任何值
xxxxxxxxxx
# 字典的特征
- 字典是序列类型,但是是无序序列,所以没有分片和索引
- 字典中的数据每个都是键值对组成,即 k T 对
- key :必须是可哈希的值,比如 int, string, float, tple,但是 list, set, dict 不可以
- value: 任何值
Type Markdown and LaTeX: α2α2
xxxxxxxxxx
# 字典常见操作
Type Markdown and LaTeX: α2α2
In [59]:
d
xxxxxxxxxx
# 访问数据
d = {"one":1,"tow":2,"three":3}
# 注意访问格式
# 中括号内是键值
print(d["one"])
d["one"] = "eins"
print(d)
# 删除某个操作
# 使用 del 操作
del d["one"]
print(d)
1 {'one': 'eins', 'tow': 2, 'three': 3} {'tow': 2, 'three': 3}
. . .
In [62]:
# 成员检测,检测的是key 内容
# 成员检测, in, not in
# 成员检测,检测的是key 内容
d = {"one":1,"tow":2,"three":3}
if 2 in d:
print("value")
if "tow" in d:
print("key")
if ("tow",2) in d:
print("kv")
key
. . .
In [66]:
注意以下特殊用法
# 便利在 python2 和python3 中区别较大,代码不通用
# 按 key 来使用 for 循环
d = {"one":1,"tow":2,"three":3}
# 使用 for 循环,直接按 key 值访问
for k in d:
print(k, d[k])
# 上述代码可以改写成如下代码
for k in d.keys():
print(k, d[k])
# 只访问字典的值
for v in d.values():
print(v)
# 注意以下特殊用法
for k,v in d:
print(k,"--",v)
one 1 tow 2 three 3 one 1 tow 2 three 3 1 2 3
. . .
xxxxxxxxxx
# 字典生成式
字典生成式¶
In [71]:
d = {"one":1,"two":2,"three":3}
x
d = {"one":1,"two":2,"three":3}
# 常规字典生成式
dd = {k:v for k,v in d.items()}
print(dd)
# 加限制条件的字典生成式
dd = {k:v for k,v in d.items() if v % 2 ==0}
print(dd)
{'one': 1, 'two': 2, 'three': 3} {'two': 2}
. . .
# 字典相关函数
字典相关函数¶
In [73]:
d = {"one":1,"two":2,"three":3}
x
# 通用函数:len, max, min, dict
# *str (字典):返回字典的字符串格式
d = {"one":1,"two":2,"three":3}
print(str(d))
{'one': 1, 'two': 2, 'three': 3}
. . .
In [75]:
it
# clear: 清空字典
# items:返回字典的键值对 组成的元祖格式
d = {"one":1,"two":2,"three":3}
i = d.items()
print(type(i))
print(i)
<class 'dict_items'> dict_items([('one', 1), ('two', 2), ('three', 3)])
. . .
In [77]:
k = d.keys() print(type(k)) print(k)
x
# keys : 返回字典的键值组成的一个结构
k = d.keys()
print(type(k))
print(k)
<class 'dict_keys'> dict_keys(['one', 'two', 'three'])
. . .
In [78]:
values
# values: 同理 ,返回一个可迭代的结构
k = d.values()
print(type(k))
print(k)
<class 'dict_values'> dict_values([1, 2, 3])
. . .
In [80]:
,300
x
# get : 根据指定键返回相应的值,好处式,可以设默认值
d = {"one":1,"two":2,"three":3}
print(d.get("on333"))
# get 默认值是 None,可以设置
print(d.get("one",200))
print(d.get("on333",300))
# 体会以下代码跟上面的代码的区别
print(d["on333"])
None 1 300
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) <ipython-input-80-d0b504524683> in <module>() 7 print(d.get("on333",300)) 8 # 体会以下代码跟上面的代码的区别 ----> 9 print(d["on333"]) KeyError: 'on333'
. . .
In [81]:
d
xxxxxxxxxx
# fromkeys:使用指定的序列作为键,使用一个值作为字典的所有的键的值
l = ["eins","zwei","drei"]
# 注意 fromkeys 两个参数的类型
# 注意 fromkeys 的调用主体
d = dict.fromkeys(l,"heheheh")
print(d)
{'eins': 'heheheh', 'zwei': 'heheheh', 'drei': 'heheheh'}