内建数据结构
内建数据结构:元组;列表、字典、集合
Tuple
In [1]: tup1=(1,2,['a','b','c'],4)
In [2]: tup2=tup1*3
In [3]: id(tup2)==id(tup1)
Out[3]: False
In [4]: tup2
Out[4]: (1, 2, ['a', 'b', 'c'], 4, 1, 2, ['a', 'b', 'c'], 4, 1, 2, ['a', 'b', 'c'], 4)
In [5]: tup1[2].append('d') # 牵一发而动全身
In [6]: tup2
Out[6]:
(1,
2,
['a', 'b', 'c', 'd'],
4,
1,
2,
['a', 'b', 'c', 'd'],
4,
1,
2,
['a', 'b', 'c', 'd'],
4)
元组拆包
:
In [8]: a,b,c,d=tup1
In [10]: c
Out[10]: ['a', 'b', 'c', 'd']
In [11]: d
Out[11]: 4
In [12]: a,b,*args=tup1
# a,b,*_=tup1
In [13]: args[0] # 剩余变量,如若之后数据无用一般约定为下划线
Out[13]: ['a', 'b', 'c', 'd']
In [14]: args[1]
Out[14]: 4
- count()
- index()
List
操作 | desp |
---|---|
append() remove() | 尾插头删 |
insert() pop() | 制定位置 |
copy() | 新开辟内存复制 Return a shallow copy of the list. |
extend() | Extend list by appending elements from the iterable. |
reverse() count() clear() sort() index() |
In [25]: list1=[1,[2,3],4]
In [26]: list2=list1
In [27]: list3=list1.copy()
In [28]: id(list1)
Out[28]: 1725205908744
In [29]: id(list2)
Out[29]: 1725205908744
In [30]: id(list3)
Out[30]: 1725216706056
In [31]: list2
Out[31]: [1, [2, 3], 4]
In [32]: list3
Out[32]: [1, [2, 3], 4]
# 浅拷贝
# 仅拷贝父对象,不会拷贝对象的内部的子对象。
# Return a shallow copy of the list.
In [37]: list1[1].append('x')
In [38]: list1
Out[38]: [1, [2, 3, 'x'], 4, 5]
In [39]: list3
Out[39]: [1, [2, 3, 'x'], 4]
In [40]: import copy
# 深拷贝
In [41]: list4=copy.deepcopy(list1)
In [42]: id(list1[1])
Out[42]: 1725203631816
In [43]: id(list3[1])
Out[43]: 1725203631816
In [44]: id(list4[1])
Out[44]: 1725233405704
通过添加内容来连接到列表是一种高代价的操作,这是因为 连接过程中创建了新的列表,并且还要复制对象。
# 推荐,更高效
everything=[]
for chunk in list_of_lists:
everything.extend(chunk)
# 操作简单
everything=[]
for chunk in list_of_lists:
everything+=chunk;
二分检索与二分插入
bisect
模块 后续
import bisect
bisect.bisect(list1,4) # 返回元素4应该插入的位置
bisect.insort(list1,4) # 将元素4插入对应位置
bisect
模块函数不会检查列表是否有序(代价)
因此,对未排序列表使用bisect
的函数虽然不会报错,但是可能会导致不正确的结果。
内建序列函数
- enumerate : 遍历时同时获取索引值 for i,value in enumerate(list): pass
- sorted
- zip(list1,list2) : 将list1与list2中元素配对,取决于最短列表
- reserved
itertools.zip_longest(list1,list2)
In [46]: for i,value in enumerate(list1):
...: print("{0} : {1} ".format(i,value))
...:
...:
0 : 1
1 : [2, 3, 'x']
2 : 4
3 : 5
In [47]: list1=['a','b','c']
In [48]: list2=[1,2,3,4,5,6]
In [49]: list3=zip(list1,list2)
In [50]: list3
Out[50]: <zip at 0x191afcd0948>
In [51]: list(list3)
Out[51]: [('a', 1), ('b', 2), ('c', 3)]
In [52]: type(list3)
Out[52]: zip
Dict
hashMap 哈希表
mapping=dict(zip(list1,list2))
value=mapping.get(key,default_value) # key not in mapping ? default :mapping[key];
setdefault
与 defaultdict
将words按首字母分类构建哈希表
常规
In [3]: words=['listen','speak','read','write','insert','re
...: move','alter','search']
In [4]: by_letter={}
In [5]: for wd in words:
...: letter=wd[0]
...: if letter not in by_letter:
...: by_letter[letter]=[wd]
...: else:
...: by_letter[letter].append(wd)
...:
In [6]: by_letter
Out[6]:
{'l': ['listen'],
's': ['speak', 'search'],
'r': ['read', 'remove'],
'w': ['write'],
'i': ['insert'],
'a': ['alter']}
setdefault
Insert key with a value of default if key is not in the dictionary.
Return the value for key if key is in the dictionary, else default.
In [7]: by_letter={}
In [8]: for wd in words:
...: letter=wd[0]
...: by_letter.setdefault(letter,[]).append(wd)
...:
In [9]: by_letter
Out[9]:
{'l': ['listen'],
's': ['speak', 'search'],
'r': ['read', 'remove'],
'w': ['write'],
'i': ['insert'],
'a': ['alter']}
insert the letter(首字母) in the dict 如果首字母不在dict则以默认list[]返回;否则返回letter所对应的list
finally append(the new word)
defaultdict
defaultdict(default_factory[, ...]) --> dict with default factory(值的数据类型)
The default factory is called without arguments to produce a new value when a key is not present, in __getitem__ only.
A defaultdict compares equal to a dict with the same items.
All remaining arguments are treated the same as if they were passed to the dict constructor, including keyword arguments.
In [11]: from collections import defaultdict
In [12]: by_letter=defaultdict(list)
In [17]: for wd in words:
...: by_letter[wd[0]].append(wd)
...:
In [18]: by_letter
Out[18]:
defaultdict(list,
{'l': ['listen'],
's': ['speak', 'search'],
'r': ['read', 'remove'],
'w': ['write'],
'i': ['insert'],
'a': ['alter']})
新的实现方式itertools模块的groupby
列表推导式
important!!!
In [2]: some_tupls=((-1,-2,3),(2,3,-5))
In [3]: tupls=[x for tup in some_tupls for x in tup if x>0]
In [4]: tupls
Out[4]: [3, 2, 3]
函数
生成器 迭代器
r=range(1,10,2)
for x in r:
print(x)
def mrange(start,stop,step=1):
i=start
while(i<stop):
yield i
i+=step