Python3 表示‘组’的概念与定义
Posted Sudo高
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python3 表示‘组’的概念与定义相关的知识,希望对你有一定的参考价值。
现实世界中总存在一组一组的事物,
一、列表的定义
type([\'hello\',\'world\',1,9,True,False]) = <class \'list\'> type([[1,2,3,],[1,2],[True,False]]) =<class \'list\'> 嵌套列表
二、列表的基本操作
[\'第一个数\',\'第二个数\',\'第三个数\',\'第四个数\'][-1]=\'第四个数\' [\'第一个数\',\'第二个数\',\'第三个数\',\'第四个数\'][-2:]=[\'第三个数\', \'第四个数\'] [\'第一个数\',\'第二个数\',\'第三个数\',\'第四个数\']+[\'另外一个数\',\'另外第二个数\']=[\'第一个数\', \'第二个数\', \'第三个数\', \'第四个数\', \'另外一个数\', \'另外第二个数\'] [\'第一个数\',\'第二个数\',\'第三个数\',\'第四个数\']*3=[\'第一个数\', \'第二个数\', \'第三个数\', \'第四个数\', \'第一个数\', \'第二个数\', \'第三个数\', \'第四个数\', \'第一个数\', \'第二个数\', \'第三个数\', \'第四个数\']
列表的操作有哪些?
import random as ran lis =[y for y in range(10)] # lis[1]=10 # print(lis) # [0, 10, 2, 3, 4, 5, 6, 7, 8, 9] # lis[2:5]=list("hello") # print(lis) # [0, 1, \'h\', \'e\', \'l\', \'l\', \'o\', 5, 6, 7, 8, 9] # lis.append(\'hello\') # print(lis) #[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, \'hello\'] # del lis[:] # print(lis) #[] # lis=lis.count(9) #这个 list 里面有多少个 \'hello\' # print(lis) # 1 列表中有一个9 # print(lis) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] # print(lis.pop()) # 9 删除最后一个 # print(lis.pop(1)) # 1 # print(lis) # [0, 2, 3, 4, 5, 6, 7, 8] # lis.remove(5) # 移除第一个 # print(lis) # [0, 1, 2, 3, 4, 6, 7, 8, 9] # lis.reverse() # 列表反转 # print(lis) # [9, 8, 7, 6, 5, 4, 3, 2, 1, 0] # lis.clear() # 清除列表 # print(lis) # [] # b =lis.copy() # print(b) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] # lis=[x for x in range(10) if x%2 ==0] # lis_extend=[x for x in range(10) if x%2 ==1] # print(lis) # [0, 2, 4, 6, 8] # print(lis_extend) # [1, 3, 5, 7, 9] # # lis.extend(lis_extend) # 追加列表 原来列表变化 + 号处理后不改变原来列表 # print(lis) # [0, 2, 4, 6, 8, 1, 3, 5, 7, 9] # lis.insert(0,\'hello\') # print(lis) # [\'hello\', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9] # lis.insert(0,list([1,2,3,])) # print(lis) # [[1, 2, 3], 0, 1, 2, 3, 4, 5, 6, 7, 8, 9] # lis =[round(ran.random()*11,2) for x in range(10)] # print(lis) # [8.39, 8.61, 9.59, 9.23, 7.41, 6.91, 7.93, 7.41, 4.75, 5.9] # lis.sort(reverse=True) #reverse True 是倒序 # print(lis) # [9.59, 9.23, 8.61, 8.39, 7.93, 7.41, 7.41, 6.91, 5.9, 4.75]
三、元组
(1, 2, 3, 4, 5) (1, -1, True) (1,2,3,4,5)[2]=3 (1,2,3,4,5)[:2]=(1, 2) (1,2,3,4,5)+(6,7)=(1, 2, 3, 4, 5, 6, 7) (1,2,3,4,5)*3=(1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5)、 type((1,2,3))=<class \'tuple\'> type((1))=<class \'int\'> type((1,))=<class \'tuple\'> type(())=<class \'tuple\'> type(("hello"))=<class \'str\'> ()括号 定义运算 所以当括号只有一个元素时候 括号当作是运算符号 而不是元祖的标识
元组的具体操作有哪些?
元组 ,不可变。不能修改,
a=[1,2,3,4] b=\'hello\' print(tuple(a)) # (1, 2, 3, 4) print(tuple(b)) # (\'h\', \'e\', \'l\', \'l\', \'o\')
四、序列总结
有序 int, float, bool, str, list, tuple str, list, tuple 序列 共有操作 每个序列元素都有一个序号 切片 [\'第一个数\',\'第二个数\',\'第三个数\',\'第四个数\'][-2:]=[\'第三个数\', \'第四个数\'] 是否包含元素 3 in [1,2,3,4,5,6] =True 3 not in [1,2,3,4,5,6]=False + * 操作 len([1,2,3,4,5,6])=6 len("hello world")=11 max([1,2,3,4,5,6])=6 min([1,2,3,4,5,6])=1 max("hello world")=“w” ord() 接受一个参数 转换成为ascll 码 ord("w")=119 ord(" ")=32
五、set 集合 {}
无序,不支持序列操作 type({1,2,3,4,5,6})=<class \'set\'> 不重复性 {1,1,2,2,3,3,4,4}={1, 2, 3, 4} len({1,2,3,4,5,6})=6 in {1,2,3,4,5,6}=True not in {1,2,3,4,5,6}=False 求两个集合差集 {1,2,3,4,5,6} - {3,4} = {1, 2, 5, 6} 求两个集合的交集 {1,2,3,4,5,6} & {3,4} = {3, 4} 求两个集合的合集 {1,2,3,4,5,6} | {3,4,7} = {1, 2, 3, 4, 5, 6, 7} 如何定义空的集合 set()
六、dict 字典
dic=[(\'name\',\'shiguang\'),(\'学号\',10086)] print(dict(dic)) # {\'name\': \'shiguang\', \'学号\': 10086} print(dict(name="拾光",age=18)) # {\'name\': \'拾光\', \'age\': 18}
字典的一些操作:
# 字典 # dic={\'name\': \'shiguang\', \'学号\': 10086} # 修改 查找 # dic[\'name\']=\'hello\' # print(dic[\'name\']) # hello # del 删除 # del dic # del dic[\'name\'] # print(dic) # {\'学号\': 10086} #清空 # dic.clear() # print(dic) #{} # print(\'我的名字叫: %(name)r 学号:%(学号)s \'%dic) # 我的名字叫: \'shiguang\' 学号:10086 # 设置默认值 # dic.setdefault(\'abcd\',True) # print(dic[\'abcd\']) # True dic={\'name\': \'shiguang\', \'学号\': 10086,\'info\':[\'name\',19,18]} dic_copy=dic.copy() # 浅复制 # dic[\'age\']=18 # print(dic) # {\'name\': \'shiguang\', \'学号\': 10086, \'info\': [\'name\', 19, 18], \'age\': 18} # print(dic_copy) # {\'name\': \'shiguang\', \'学号\': 10086, \'info\': [\'name\', 19, 18]} # dic[\'info\'].remove(\'name\') #两个都会删除 # del dic[\'name\'] #copy的字典不会删除 # print(dic) # {\'学号\': 10086, \'info\': [19, 18]} # print(dic_copy) # {\'name\': \'shiguang\', \'学号\': 10086, \'info\': [19, 18]} # 深复制 from copy import deepcopy dic_copy=deepcopy(dic) dic[\'info\'].remove(\'name\') # deepcopy的name不会删除 del dic[\'name\'] # deepcopy的字典不会删除 print(dic) # {\'学号\': 10086, \'info\': [19, 18]} print(dic_copy) # {\'name\': \'shiguang\', \'学号\': 10086, \'info\': [\'name\', 19, 18]}
Key 不可变的类型 Value 可以使任何类型 字典定义形式 {key1:value1,key2:value2...} type({1:1,2:2,3:3}) = <class \'dict\'> {\'Q\':\'q技能\',\'W\':\'w技能\',\'E\':\'e技能\',\'R\':\'r技能\'} 通过key 来访问/得到 value 字典不能有相同的key {\'Q\':\'q技能\',\'W\':\'w技能\',\'E\':\'e技能\',\'R\':\'r技能\'}[\'Q\'] = \'q技能\' {1:\'q技能\',\'1\':\'w技能\',\'E\':\'e技能\',\'R\':\'r技能\'}[\'1\'] = \'w技能\'
七、思维导图总结基本数据类型
以上是关于Python3 表示‘组’的概念与定义的主要内容,如果未能解决你的问题,请参考以下文章
SpringCloud系列十一:SpringCloudStream(SpringCloudStream 简介创建消息生产者创建消息消费者自定义消息通道分组与持久化设置 RoutingKey)(代码片段