对于元组的函数
- 以下看代码
- 以下函数,对list根本实用
# len:获取元组的长度t = (1,2,3,4,5)len(t)
5
# max,min:最大最小值print(max(t))print(min(t))
51
# tuple:转化或创立元组l = (1,2,3,4,5)t = tuple(l)print(t)t = tuple()print(t)
(1, 2, 3, 4, 5)()
元组的函数
- 根本跟list通用
# count:计算指定数据呈现的次数t = (2,1,2,3,45,1,1,2,)print(t.count(2))# index:求指定元素在元组中的索引地位print(t.index(45))# 如果须要的查找的数字是多个,则返回第一个print(t.index(1))
341
元组变量交换法
- 两个变量替换值
# 两个变量替换值a = 1b = 3print(a)print(b)print("*" * 20)# java程序员会这么写:c = aa = bb = cprint(a)print(b)print("*" * 20)# python写法a,b = b,aprint(a)print(b)
13********************31********************13
汇合-set
- 汇合是高中数学中的一个概念
- 一堆确定的无序的惟一的数据,汇合中每一个数据成为一个元素
# 汇合的定义s = set()print(type(s))print(s)# 此时,大括号内肯定要有值,否则定义出的是一个dicts = {1,2,3,4,5,6,7}print(type(s))print(s)
<class 'set'>set()<class 'set'>{1, 2, 3, 4, 5, 6, 7}
# 如果只是用大括号定义,则定义的是一个dict类型d = {}print(type(d))print(d)
<class 'dict'>{}
汇合的特色
- 汇合的数据无序,即无奈应用索引和分片
- 汇合外部数据元素具备唯一性,能够用来排除反复数据
- 汇合内的数据,str,int,float,tuple,冰冻汇合等,即外部只能搁置可哈希数据
汇合序列操作
# 成员检测# in,not ins = {4,5,"i", "love", "you"}print(s)if "love" in s: print("Yes") if "haha" not in s: print("Yes")
{'you', 4, 5, 'love', 'i'}YesYes
汇合遍历操作
# for 循环s = {4,5,"i", "love", "you"}for i in s: print(i)
you45lovei
# 带有元组的汇合遍历s = {(1,2,3,), ("i", "love", "you"), (4,5,6)}for k,m,n in s: print(k, "--", m, "--", n) for k in s: print(k)
i -- love -- you4 -- 5 -- 61 -- 2 -- 3('i', 'love', 'you')(4, 5, 6)(1, 2, 3)
汇合的外延
# 一般汇合外延# 以下汇合在初始化后主动过滤掉反复元素s = {23,223,233,2,4,5,6,3,4,1,5,3}print(s)# 一般汇合外延ss = {i for i in s}print(ss)
{1, 2, 3, 4, 5, 6, 233, 23, 223}{1, 2, 3, 4, 5, 6, 233, 23, 223}
# 带条件的汇合外延sss = {i for i in s if i % 2 == 0}print(sss)
{2, 4, 6}
# 多循环的汇合外延s1 = {1,2,3,4}s2 = {"i", "love", "you"}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 == 2}print(s)
{'you', 'youyou', 'love', 'lovelovelovelove', 'lovelovelove', 'lovelove', 'iii', 'youyouyouyou', 'ii', 'i', 'iiii', 'youyouyou'}{'lovelove', 'youyou', 'ii'}
汇合函数/对于汇合的函数
# len,max,min:跟其余根本的函数统一s = {23,54,72,3,5,3,3,6,1,543}print(len(s))print(max(s))print(min(s))
85431
# set:生成一个汇合l = {1,2,3,4,5,4,3,2,1}s = set(l)print(s)
{1, 2, 3, 4, 5}
# add:向汇合内增加元素s = {1}s.add(3)print(s)
{1, 3}
# clears = {1,2,3,4,5}print(id(s))s.clear()print(id(s))# 结果表明clear函数是原地清空数据
13707738435281370773843528
# copy:拷贝# remove:移除指定的值,间接扭转原有值,如果要删除的值不存在,报错# discard:移除汇合中指定的值跟remove一样,然而如果要删除的话,不报错s = {23,4,3,5,1,2,3}s.remove(4)print(s)s.discard(1)print(s)print("*" * 20)s.discard(100)print(s)s.remove(100)print(s)
{1, 2, 3, 5, 23}{2, 3, 5, 23}********************{2, 3, 5, 23}---------------------------------------------------------------------------KeyError Traceback (most recent call last)<ipython-input-35-0113522ad176> in <module> 12 print(s) 13 ---> 14 s.remove(100) 15 print(s)KeyError: 100
# pop 随机移除一个元素s = {1,2,3,4,5,6,7}d = s.pop()print(d)print(s)
1{2, 3, 4, 5, 6, 7}
# 汇合函数# intersection:交加# difference:差集# union:并集# issubset:查看一个汇合是否为另一个子集# issuperset:查看一个汇合是否为另一个超集s1 = {1,2,3,4,5,6}s2 = {5,6,7,8,9}s_1 = s1.intersection(s2)print(s_1)s_2 = s1.difference(s2)print(s_2)s_3 = s1.issubset(s2)print(s_3)s_4 = s1.issuperset(s2)print(s_4)
{5, 6}{1, 2, 3, 4}FalseFalse
# 汇合数学操作s1 = {1,2,3,4,5,6}s2 = {5,6,7,8,9}# 以下不反对s_1 = s1 - s2print(s_1)s_2 = s1 + s2print(s_2)
{1, 2, 3, 4}---------------------------------------------------------------------------TypeError Traceback (most recent call last)<ipython-input-45-fac787d752ea> in <module> 7 print(s_1) 8 ----> 9 s_2 = s1 + s2 10 print(s_2)TypeError: unsupported operand type(s) for +: 'set' and 'set'
frozen set :冰冻汇合
- 冰冻就是不能够进行任何批改的操作
- frozenset是一种非凡汇合
# 创立s = frozenset()print(type(s))print(s)
<class 'frozenset'>frozenset()
dict字典
- 字典是一种组合数据,没有程序的组合数据,数据以键值对模式呈现
# 字典的创立# 创立空字典1d = {}print(type(d))print(d)# 创立空字典2d = dict()print(d)# 创立有值的字典,每一组数据用冒号隔开,每一对键值对用逗号隔开d = {"one":1, "two":2, "three":3}print(d)# 用dict创立有内容字典1d = dict({"one":1, "two":2, "three":3})print(d)# 用dict创立有内字典2# 利用要害参数d = dict(one=1, two=2, three=3)print(d)# d = dict( [("one",1), ("two",2), ("three",3)])print(d)
<class 'dict'>{}{}{'one': 1, 'two': 2, 'three': 3}{'one': 1, 'two': 2, 'three': 3}{'one': 1, 'two': 2, 'three': 3}{'one': 1, 'two': 2, 'three': 3}
字典的个性
- 字典是序列类型,然而是无序序列,所以没有分片和索引
字典中的数据每个都有键值对组成,即kv对
- key:必须是可哈希的值,比方:int,string,float,tuple,然而,list,set,dict不行
- value:任何值
字典常见操作
# 拜访数据d = {"one":1, "two":2, "three":3}# 留神拜访格局# 中括号内是键值print(d["one"])d["one"] = "eins"print(d)# 删除某个操作# 应用del操作del d["one"]print(d)
1{'one': 'eins', 'two': 2, 'three': 3}{'two': 2, 'three': 3}
# 成员检测:in,not in# 成员检测检测时的key内容d = {"one":1, "two":2, "three":3}if 2 in d: print("value")if "two" in d: print("key") if ("two,2") in d: print("kv")
key
# 遍历在python2 和 3 中区别比拟大,代码不通用# 按key值应用for循环d = {"one":1, "two":2, "three":3}# 应用for循环,间接按keu值拜访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.items(): print(k, "--", v)
one 1two 2three 3one 1two 2three 3123one -- 1two -- 2three -- 3
字典生成式
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}
字典相干函数
# 通用函数:len,max,min,dict# str(字典):用于返回字典的字符串格局d = {"one":1, "two":2, "three":3}print(str(d))
{'one': 1, 'two': 2, 'three': 3}
# 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)])
# keys:返回字典的键组成的一个构造k = d.keys()print(type(k))print(k)
<class 'dict_keys'>dict_keys(['one', 'two', 'three'])
# values:同理,一个可迭代的构造v = d.values()print(type(v))print(v)
<class 'dict_values'>dict_values([1, 2, 3])
# get:依据指定键返回相应的值,益处是,能够生成默认值d = {"one":1, "two":2, "three":3}print(d.get("oner"))# get默认值是None,能够设置print(d.get("one", 100))print(d.get("one33", 100))print(d['on333'])
None1100---------------------------------------------------------------------------KeyError Traceback (most recent call last)<ipython-input-86-f8c01a58018e> in <module> 8 print(d.get("one33", 100)) 9 ---> 10 print(d['on333'])KeyError: 'on333'
# fromkeys:应用指定的序列作为键,应用一个值作为字典的所有键的值l = ["eins", "zwei", "dree"]# 留神fromkeys两个参数的类型# 留神fromkeys的调用主体d = dict.fromkeys(l, "hahahahaha")print(d)
{'eins': 'hahahahaha', 'zwei': 'hahahahaha', 'dree': 'hahahahaha'}