关于元组的函数
t = (1,2,3,4,5)
len(t)
5
print(max(t))
print(min(t))
5
1
l = (1,2,3,4,5)
t = tuple(l)
print(t)
t = tuple()
print(t)
(1, 2, 3, 4, 5)
()
元组的函数
t = (2,1,2,3,45,1,1,2,)
print(t.count(2))
print(t.index(45))
print(t.index(1))
3
4
1
元组变量交换法
a = 1
b = 3
print(a)
print(b)
print("*" * 20)
c = a
a = b
b = c
print(a)
print(b)
print("*" * 20)
a,b = b,a
print(a)
print(b)
1
3
********************
3
1
********************
1
3
集合-set
- 集合是高中数学中的一个概念
- 一堆确定的无序的唯一的数据,集合中每一个数据成为一个元素
s = set()
print(type(s))
print(s)
s = {1,2,3,4,5,6,7}
print(type(s))
print(s)
<class 'set'>
set()
<class 'set'>
{1, 2, 3, 4, 5, 6, 7}
d = {}
print(type(d))
print(d)
<class 'dict'>
{}
集合的特征
- 集合的数据无序,即无法使用索引和分片
- 集合内部数据元素具有唯一性,可以用来排除重复数据
- 集合内的数据,str,int,float,tuple,冰冻集合等,即内部只能放置可哈希数据
集合序列操作
s = {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'}
Yes
Yes
集合遍历操作
s = {4,5,"i", "love", "you"}
for i in s:
print(i)
you
4
5
love
i
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
4
1
('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'}
集合函数/关于集合的函数
s = {23,54,72,3,5,3,3,6,1,543}
print(len(s))
print(max(s))
print(min(s))
8
543
1
l = {1,2,3,4,5,4,3,2,1}
s = set(l)
print(s)
{1, 2, 3, 4, 5}
s = {1}
s.add(3)
print(s)
{1, 3}
s = {1,2,3,4,5}
print(id(s))
s.clear()
print(id(s))
1370773843528
1370773843528
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
s = {1,2,3,4,5,6,7}
d = s.pop()
print(d)
print(s)
1
{2, 3, 4, 5, 6, 7}
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}
False
False
s1 = {1,2,3,4,5,6}
s2 = {5,6,7,8,9}
s_1 = s1 - s2
print(s_1)
s_2 = s1 + s2
print(s_2)
{1, 2, 3, 4}
TypeError Traceback (most recent call last)
<ipython-input-45-fac787d752ea> in <module>
7 print(s_1)
8
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字典
- 字典是一种组合数据,没有顺序的组合数据,数据以键值对形式出现
d = {}
print(type(d))
print(d)
d = dict()
print(d)
d = {"one":1, "two":2, "three":3}
print(d)
d = dict({"one":1, "two":2, "three":3})
print(d)
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 d["one"]
print(d)
1
{'one': 'eins', 'two': 2, 'three': 3}
{'two': 2, 'three': 3}
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
d = {"one":1, "two":2, "three":3}
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 1
two 2
three 3
one 1
two 2
three 3
1
2
3
one
two
three
字典生成式
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}
字典相关函数
d = {"one":1, "two":2, "three":3}
print(str(d))
{'one': 1, 'two': 2, 'three': 3}
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)])
k = d.keys()
print(type(k))
print(k)
<class 'dict_keys'>
dict_keys(['one', 'two', 'three'])
v = d.values()
print(type(v))
print(v)
<class 'dict_values'>
dict_values([1, 2, 3])
d = {"one":1, "two":2, "three":3}
print(d.get("oner"))
print(d.get("one", 100))
print(d.get("one33", 100))
print(d['on333'])
None
1
100
KeyError Traceback (most recent call last)
<ipython-input-86-f8c01a58018e> in <module>
8 print(d.get("one33", 100))
9
KeyError: 'on333'
l = ["eins", "zwei", "dree"]
d = dict.fromkeys(l, "hahahahaha")
print(d)
{'eins': 'hahahahaha', 'zwei': 'hahahahaha', 'dree': 'hahahahaha'}