python 学习 序列

发布时间:2026/7/13 6:18:23
python 学习 序列 array数组的应用和列表的区别是只能存储相同类型的数据下面type code 是指定数据类型的字符importbisect inter_list[]bisect.insort(inter_list,3)#真实插入元素的动作bisect.insort(inter_list,2)bisect.insort(inter_list,5)bisect.insort(inter_list,1)bisect.insort(inter_list,6)print(inter_list)#1, 2, 3, 5, 6]#下面是假如插入的话索引是几#若插入到和已经存在元素相等的中间元素print(bisect.bisect(inter_list,3))#若插入到相等元素的右边是位置的索引就是3print(bisect.bisect_left(inter_list,3))#若插入到相等元素的左边边是位置的索引就是2#若插入到和已经存在元素相等的开始或者末尾print(bisect.bisect(inter_list,6))#若插入到相等元素的右边是位置的索引就是5print(bisect.bisect_left(inter_list,6))#若插入到相等元素的左边边是位置的索引就是4#若插入不存在的元素print(bisect.bisect(inter_list,4))#若插入到刚刚大于的元素的右边位置的索引的3print(bisect.bisect_left(inter_list,4))#若插入到刚刚大于的元素的右边位置的索引的3print(bisect.bisect(inter_list,0))#若插入到刚刚大于的元素的右边位置的索引的3print(bisect.bisect_left(inter_list,0))#若插入到刚刚大于的元素的右边位置的索引的3importarray my_arrayarray.array(i)#指定存储的类型my_array.append(1)my_array.extend((4,5))print(my_array)#array(i, [1, 4, 5])importcollections queuecollections.deque([1,2,3,4])queue.append(5)queue.appendleft(6)queue.extend([7,8])queue.extendleft([9,10])#在做面先插入9然后10 [10, 9, 6, 1, 2, 3, 4, 5, 7, 8]queue.pop()queue.popleft()#[9, 6, 1, 2, 3, 4, 5, 7]queue.rotate(2)#[5, 7, 9, 6, 1, 2, 3, 4] 后面的57跑到前面去了queue.rotate(-2)#[9, 6, 1, 2, 3, 4, 5, 7]print(queue)queue1collections.deque(maxlen3)#空的queue,最多只能放3个 deque([])queue1.extendleft([a,b,c,d,e])#deque([e, d, c], maxlen3) 最多存放最后的三个print(queue1)# 列表生成式odd_list[iforiinrange(11)ifi%21]print(odd_list)#生成器表达式odd_list(iforiinrange(11)ifi%21)print(odd_list)forobjinodd_list:print(obj)#字典推导式my_dict{wzg:1,wyn:4,wxr:6}reversed_dict{value*value:keyforkey,valueinmy_dict.items()}print(reversed_dict)#集合推导式my_set{keyforkeyinmy_dict.keys()}print(my_set)my_setset(my_dict.keys())print(my_set)