Python入门学习笔记————09(元组,集合,字典)

关于元组的函数

  • 以下看代码

In [5]:

#len:获取元组长度1
t = (1,2,3,4,4)
len(t)

Out[5]:

5

In [6]:

#max,min:最大值,最小值
#如果元祖中有多个最大值最小值
print(max(t))
print(min(t))
4
1

In [8]:

#tuple:转化或创建元组
l = [1,2,3,4]
t = tuple(l)
print(t)
t = tuple()
print(t)
(1, 2, 3, 4)
()

元组的函数

  • 基本与list通用

In [12]:

# count : 计算指定数据出现的次数
t = (2,1,2,2,6,7,8,3,3)
print(t.count(2))
# index:求指定元素在元组中的索引位置
print(t.index(8))
#如果需要查找的数字是多个,则返回第一个
print(t.index(3))
3
6
7

元组变量交换法

  • 两个变量值交换顺序

In [15]:

a = 1
b = 3
print(a)
print(b)
print('*'*20)
#java程序员
c = a
a = b
b = c
print(a)
print(b)
print ('*'*20)
#pyton程序员
a,b = b,a
print(a)
print(b)
1
3
********************
3
1
********************
1
3

集合 -set

  • 高中数学中的一种概念
  • 一堆确定的无序的唯一的数据,集合中每一个数据成为一个元素

In [20]:

 # 集合的定义
s = set()
print(type(s))
print(s)
#此时大括号里必须有值,否则定义的是dict 
s = {1,2,3,4,5}
print(s)
<class 'set'>
set()
{1, 2, 3, 4, 5}

In [19]:

#如果只是用大括号定义,则定义的是一个dict类型
d = {}
print(type(d))
print(d)
<class 'dict'>
{}

集合的特征

  • 集合内数据无序,即无法使用索引和分片
  • 集合内部数据元素具有唯一性,可以用来排除重复数据
  • 集合内的数据,str, int, float,tuple,冰冻集合等,及内部只能放置可哈希数据

集合序列操作

In [23]:

 
#成员检测
#in 或 not in
s = {3,4,5,'hello','good','love'}
print(s)
if 'hello' in s:
    print('ni hao')
if 'gou dan' not in s:
    print('bye')
{'good', 3, 'hello', 4, 5, 'love'}
ni hao
bye

集合的便利操作

In [25]:

# for 循环
s = {3,4,5,'hello','good','love'}
for i in s:
    print(i,end=' ')
good 3 hello 4 5 love 

In [27]:

# 带元组的集合遍历
s = {(1,2,3),('hello','ko','good'),(22,33,5)}
for k,v,m in s :
    print(k,'---',v,'---',m,'---')
for k in s:
    print(k)
22 --- 33 --- 5 ---
hello --- ko --- good ---
1 --- 2 --- 3 ---
(22, 33, 5)
('hello', 'ko', 'good')
(1, 2, 3)

集合的内函数

In [29]:

#普通集合函数
s = {11,2,3,55,76,4,2,33,2}
print(s)
#普通集合内函数
ss = {i for i in s}
print(ss)
{33, 2, 3, 4, 11, 76, 55}
{33, 2, 3, 4, 11, 76, 55}

In [30]:

#带条件的集合内函数
sss = {i for i in s if i % 2 == 0}
print(sss)
{2, 4, 76}

In [34]:

#多循环的集合内涵
s1 = {1,2,3}
s2 = {'hello','hhh','ds'}
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)
{'hellohello', 'hello', 'hhh', 'hellohellohello', 'hhhhhh', 'dsds', 'dsdsds', 'ds', 'hhhhhhhhh'}
{'hhhhhh', 'hellohello', 'dsds'}

集合函数、关于集合的函数

In [37]:

# len  max  min  跟其他基本函数一致
s = {22,66,234,13,33,6,2}
print(len(s))
print(max(s))
print(min(s))
7
234
2

In [39]:

# set :生成一个集合
l = [2,33,4,6,7,4,22]
s = set (l)
print(s)
{33, 2, 4, 6, 7, 22}

In [40]:

# add : 向集合内添加元素
s = {1}
s.add(33)
print(s)
{1, 33}

In [43]:

# clear
s = {1,2,3,4}
print(id(s))
s.clear()
print(id(s))
140272830374856
140272830374856

In [47]:

# copy :拷贝
# remove:移除指定的值,直接改变原有值,如果要删除的词不存在,报错
# discard:移除集合中的指定值,跟remove一样但如果删除的话不报错
s = {1,23,4,6,78,3}
s.remove (4)
print(s)
s.discard(1)
print(s)
print('@'* 20)
#注意两者的区别
s.discard(1212)
print(s)
s.remove(121212)
print(s)
# 为什么remove不存在的值会报keyerror
{1, 3, 6, 78, 23}
{3, 6, 78, 23}
@@@@@@@@@@@@@@@@@@@@
{3, 6, 78, 23}
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-47-816d0dbdc858> in <module>()
     11 s.discard(1212)
     12 print(s)
---> 13 s.remove(121212)
     14 print(s)

KeyError: 121212

In [52]:

#pop 随机移除一个数
s = {1,2,3,4}
d= s.pop()
print(s)
print(d)
{2, 3, 4}
1

In [55]:

 
#集合函数
#intersection :交集
#difference:差集
#union:并集
#issubset:检查一个集合是否是另一个的子集
#issuperset:检查一个集合是否为另一个超集
s1 = {1,2,33,4,5}
s2 = {4,5,63,3,7,56}
s_1 = s1.intersection(s2)
print(s_1)
s_2 = s1.difference(s2)
print(s_2)
s_3 = s1.issubset(s2)
print(s_3)
{4, 5}
{1, 2, 33}
False

In [59]:

#集合的数学操作
s1 = {1,2,33,4,5}
s2 = {4,5,63,3,7,56}
s_1 = s1 - s2
print(s_1)
{1, 2, 33}

frozenset:冰冻集合

  • 冰冻集合就是不允许任何修改的集合
  • 是一个特殊的集合

In [60]:

#创建
s = frozenset()
print(type(s))
print(s)
<class 'frozenset'>
frozenset()

dict字典

  • 字典是一种组合数据,没有顺序的组合数据,数据以键值对形式出现

In [106]:

 
# 字典的创建
#1
d = {}
print(d)
#2
d = dict()
print(d)
#创建有值的,每一组数据用冒号隔开,每一对键值对用逗号隔开
d = {'one':1,'two':2}
print(d)
#用dict直接创建
d =( {'one':1,'two':2})
print(d)
#用dict直接创建
d =dict(one=1,two=2)
print(d)
#
d = dict([('one',1),('two', 2)])
print(d)
{}
{}
{'one': 1, 'two': 2}
{'one': 1, 'two': 2}
{'one': 1, 'two': 2}
{'one': 1, 'two': 2}

字典的特征

  • 字典是序列类型,但是是无序列表,所以没有分片和索引
  • 字典的数据每个都有键值对组成, kv
    • key:必须为可哈希的值,比如int,string,float,tuple,但是,list,set,dict不行
    • value:任何值

字典的常用操作

In [108]:

# 访问数据
d =( {'one':1,'two':2})
#注意访问格式
#中括号中的是键值
print(d['one'])
d['one']='eins'
print(d)
#删除某个操作
#使用del操作
del d['one']
print(d)
1
{'one': 'eins', 'two': 2}
{'two': 2}

In [109]:

 
# 成员检测函数  in  not in
#检测的是key的内容
d =( {'one':1,'two':2})
if 2 in d:
    print('value')
if 'one' in d:
    print('key')
if ('one',1) in d:
    print('kv')
key

In [114]:

# 遍历
d =( {'one':1,'two':2})
# 按key值使用for
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
one 1
two 2
1
2
one --- 1
two --- 2

字典生成式

In [116]:

d =( {'one':1,'two':2})
#常规字典生成式
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}
{'two': 2}

字典相关函数

In [117]:

# 通用函数:len,max,min,dict
#str(字典):返回字符串格式
d =( {'one':1,'two':2})
print( str(d))
{'one': 1, 'two': 2}

In [119]:

#clear:清空
#items:返回字典键值对组成的元组格式
d =( {'one':1,'two':2})
i = d.items()
print(type(i))
print(i)
<class 'dict_items'>
dict_items([('one', 1), ('two', 2)])

In [120]:

#keys:返回字典的键组成的一个结构
k = d.keys()
print(type(k))
print(k)
<class 'dict_keys'>
dict_keys(['one', 'two'])

In [122]:

#values:同理,一个可迭代结构
v = d.values()
print(type(v))
print(v)
<class 'dict_values'>
dict_values([1, 2])

In [126]:

#get :根据指定键返回相应的值,可以设置默认值
d =( {'one':1,'two':2})
print(d.get('oness'))
#get 默认值是None,可以进行设置
print(d.get('one33',33))
#与上进行区别
print(d['oness'])
None
33
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-126-d50959b6f39f> in <module>()
      6 print(d.get('one33',33))
      7 #与上进行区别
----> 8 print(d['oness'])

KeyError: 'oness'

In [127]:

# fromkeys:使用指定的序列作为键,使用一个值作为字典的所有的键的值
l = ['sd','ss','er']
#注意fromkeys的两个参数的类型
#注意fromkeys的调用主体
d = dict.fromkeys(l,'hahahahh')
print(d)
{'sd': 'hahahahh', 'ss': 'hahahahh', 'er': 'hahahahh'}

猜你喜欢

转载自blog.csdn.net/weixin_42492218/article/details/85260490
今日推荐