Python容器学习总结

Python容器学习总结

什么是容器?

容器是用来存储和组织其他对象的对象。

字符串

#1、字符串是不可变的
#2、使用''或者""来创建字符串
#3、可以使用下标访问字符串中的单个字符,下标从0开始
#4、可以使用[x:y]来截取字符串部分内容,不包含y下标字符
#5、拼接字符串使用+
#6、重复输出字符串使用*
#7、判断字符串是否包含给定字符串
#7、判断字符串是否不包含给定字符串
#8、格式化输出语法"我叫 %s 今年 %d 岁!" % ('小明', 10)
#9、python三引号允许一个字符串跨多行,字符串中可以包含换行符、制表符以及其他特殊字符
#10、常用字符串内建函数
# len()返回字符串长度
# lower()转换字符串中所有大写字符为小写.
# upper()转换字符串中的小写字母为大写
# replace(old, new[, max])把 将字符串中的 str1 替换成 str2,如果 max 指定,则替换不超过 max 次。

'''字符串'''
#1、字符串是不可变的
#2、使用''或者""来创建字符串
#3、可以使用下标访问字符串中的单个字符,下标从0开始
#4、可以使用[x:y]来截取字符串部分内容,不包含y下标字符
#5、拼接字符串使用+
#6、重复输出字符串使用*
#7、判断字符串是否包含给定字符串
#7、判断字符串是否不包含给定字符串
#8、格式化输出语法"我叫 %s 今年 %d 岁!" % ('小明', 10)
#9、python三引号允许一个字符串跨多行,字符串中可以包含换行符、制表符以及其他特殊字符
#10、常用字符串内建函数
# len()返回字符串长度
# lower()转换字符串中所有大写字符为小写.
# upper()转换字符串中的小写字母为大写
# replace(old, new[, max])把 将字符串中的 str1 替换成 str2,如果 max 指定,则替换不超过 max 次。
​
str1 = 'Hello'
str2 = "World"
​
print(str1[0])
print(str1[0:3])
print(str1+' '+str2+'!!')
print(str1*3)
print('he' in str1)
print('he' not in str1)
print("我叫 %s 今年 %d 岁!" % ('小明', 10))
print('''
hello world!
hello world!hello world!
hello world!hello world!hello world!
hello world!hello world!hello world!hello world!
hello world!hello world!hello world!hello world!hello world!
\t  #\n
#
''')
print(len(str1))
print(str1.lower())
print(str1.upper())
print(str1.replace('H', 'hh'))
#输出
'''
H
Hel
Hello World!!
HelloHelloHello
False
True
我叫 小明 今年 10 岁!
​
hello world!
hello world!hello world!
hello world!hello world!hello world!
hello world!hello world!hello world!hello world!
hello world!hello world!hello world!hello world!hello world!
      #
​
#
​
5
hello
HELLO
hhello
'''
​

列表

#1、序列是Python中最基本的数据结构
#2、列表可以使用下标索引访问支持负数索引,也可以进行截取
#3、更新元素可以直接修改,添加元素使用append()方法
#4、删除列表元素使用del
#5、列表对 + 和 * 的操作符与字符串相似。+ 号用于组合列表,* 号用于重复列表。
#6、列表常用函数
# list.append(obj) 在列表末尾添加新的对象
# list.count(obj) 统计某个元素在列表中出现的次数
# list.index(obj) 从列表中找出某个值第一个匹配项的索引位置
# list.insert(index, obj) 将对象插入列表
# list.pop([index=-1]) 移除列表中的一个元素(默认最后一个元素),并且返回该元素的值
# list.remove(obj)  移除列表中某个值的第一个匹配项
# list.sort( key=None, reverse=False) 对原列表进行排序
# list.clear() 清空列表

#1、序列是Python中最基本的数据结构
#2、列表可以使用下标索引访问支持负数索引,也可以进行截取
#3、更新元素可以直接修改,添加元素使用append()方法
#4、删除列表元素使用del
#5、列表对 + 和 * 的操作符与字符串相似。+ 号用于组合列表,* 号用于重复列表。
#6、列表常用函数
# list.append(obj) 在列表末尾添加新的对象
# list.count(obj) 统计某个元素在列表中出现的次数
# list.index(obj) 从列表中找出某个值第一个匹配项的索引位置
# list.insert(index, obj) 将对象插入列表
# list.pop([index=-1]) 移除列表中的一个元素(默认最后一个元素),并且返回该元素的值
# list.remove(obj)  移除列表中某个值的第一个匹配项
# list.sort( key=None, reverse=False) 对原列表进行排序
# list.clear() 清空列表
​
​
list2 = [1,2,3]
list1= ['hello',2,(3,),{'key1':'value'}]
​
print(">>>>>>>>>>>>")
print(list1[0])
print(list1[0:2])
list2[0]=10
print(list2)
list2.append(8)
print(list2)
del list2[0]
print(list2)
print(len(list2))
print(list2+list1)
print(list2*2)
print(3 in list2)
#输出
'''
>>>>>>>>>>>>
hello
['hello', 2]
[10, 2, 3]
[10, 2, 3, 8]
[2, 3, 8]
3
[2, 3, 8, 'hello', 2, (3,), {'key1': 'value'}]
[2, 3, 8, 2, 3, 8]
True
'''
​

 

元组

#1、元组的元素不能修改。
#2、元组中只包含一个元素时,需要在元素后面添加逗号,否则括号会被当作运算符使用
#3、下标索引从0开始,可以进行截取
#4、元组中的元素值是不允许修改的,但我们可以对元组进行连接组合
#5、元组中的元素值是不允许删除的,但我们可以使用del语句来删除整个元组
#6、常用的元组函数
# len(tup)  计算元素个数
# tuple(iterable) 将可迭代系列转换为元组。
# max(tuple) 返回元组中元素最大值。
# min(tuple) 返回元组中元素最小值。
print(">>>>>>>>>>>>")
tup1 = (1,)
tup2 = (2,3,4)
tup3 = tup1+tup2
tup4 = (8,7)
print(tup2)
print(tup2[1])
print(tup2[0:2])
print(tup3)
print(tup4)
del tup4
#print(tup4) 会提示错误

'''元组'''
#1、元组的元素不能修改。
#2、元组中只包含一个元素时,需要在元素后面添加逗号,否则括号会被当作运算符使用
#3、下标索引从0开始,可以进行截取
#4、元组中的元素值是不允许修改的,但我们可以对元组进行连接组合
#5、元组中的元素值是不允许删除的,但我们可以使用del语句来删除整个元组
#6、常用的元组函数
# len(tup)  计算元素个数
# tuple(iterable) 将可迭代系列转换为元组。
# max(tuple) 返回元组中元素最大值。
# min(tuple) 返回元组中元素最小值。
print(">>>>>>>>>>>>")
tup1 = (1,)
tup2 = (2,3,4)
tup3 = tup1+tup2
tup4 = (8,7)
print(tup2)
print(tup2[1])
print(tup2[0:2])
print(tup3)
print(tup4)
del tup4
#print(tup4) 会提示错误
​
#输出
'''
(2, 3, 4)
3
(2, 3)
(1, 2, 3, 4)
(8, 7) 
'''

字典

#字典是另一种可变容器模型,且可存储任意类型对象。
#键必须是唯一的,但值则不必。

'''字典'''
#字典是另一种可变容器模型,且可存储任意类型对象。
#键必须是唯一的,但值则不必。
dict1= {"aa":1,"bb":2,"cc":3}
​
print(dict1)
#修改字典中key的值
dict1['aa'] = 11
print(dict1)
#添加键值对
dict1['dd']= 4
print(dict1)
​
#删除字典键值对
del dict1['dd']
print(dict1)
​
#清空字典
#dict1.clear()
​
#删除字典
#del dict1
​
#字典常用函数
#len(dict)计算字典元素个数,即键的总数
print(len(dict1))
#key in dict 如果键在字典dict里返回true,否则返回false
print('aa' in dict1)
​
#radiansdict.keys() 返回一个迭代器,可以使用 list() 来转换为列表
for i in dict1.keys():
    print(i)
​
#radiansdict.values() 返回一个迭代器,可以使用 list() 来转换为列表
for i in dict1.values():
    print(i)
#pop(key[,default])删除字典给定键 key 所对应的值,返回值为被删除的值。key值必须给出。 否则,返回default值。
dict2 = {"ww":1,"ee":2}
print(dict2)
print(dict2.pop('ww'))
print(dict2)
​
#输出
'''
{'aa': 1, 'bb': 2, 'cc': 3}
{'aa': 11, 'bb': 2, 'cc': 3}
{'aa': 11, 'bb': 2, 'cc': 3, 'dd': 4}
{'aa': 11, 'bb': 2, 'cc': 3}
3
True
aa
bb
cc
11
2
3
{'ww': 1, 'ee': 2}
1
{'ee': 2}
'''
​
li1 = ['a','b','c']
li2 = [1,2,3]
li3= [1,2]
print(dict(zip(li1,li2)))
print(dict(zip(li1,li3)))
{'a': 1, 'b': 2, 'c': 3}
{'a': 1, 'b': 2}

集合

  1. #集合(set)是一个无序的不重复元素序列。

  2. #可以使用大括号 { } 或者 set() 函数创建集合,注意:创建一个空集合必须用 set() 而不是 { },因为 { } 是用来创建一个空字典。

'''集合'''
#集合(set)是一个无序的不重复元素序列。
#可以使用大括号 { } 或者 set() 函数创建集合,注意:创建一个空集合必须用 set() 而不是 { },因为 { } 是用来创建一个空字典。
​
s1 = {'a',1,2,'b'}
print(s1)
#创建一个空集合
s2 = set()
#判断元素是否在集合内
print('a' in s1)
​
#添加元素
s1.add('c')
print(s1)
​
#移除一个元素
s1.remove(1)
print(s1)
​
#移除集合中的元素
s1.discard('2')
print(s1)
#给集合添加元素update()
s1.update({6})
print(s1)
​
#输出
{'a', 2, 'b', 1}
True
{1, 2, 'c', 'a', 'b'}
{2, 'c', 'a', 'b'}
{2, 'c', 'a', 'b'}
{2, 'a', 6, 'b', 'c'}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值