-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy patharray_and_list.py
More file actions
executable file
·62 lines (42 loc) · 1.06 KB
/
Copy patharray_and_list.py
File metadata and controls
executable file
·62 lines (42 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env python
# -*- coding:utf-8 -*-
__author__ = 'MFC'
__time__ = '2019-07-15 11:32'
"""
lesson 4
线性结构:
1.内存连续
2.下标访问
cd ~/Python3Demo/pw_python/algorithm
pytest array_and_list.py
ref:
https://pegasuswang.github.io/python_data_structures_and_algorithms/02_%E6%95%B0%E7%BB%84%E5%92%8C%E5%88%97%E8%A1%A8/array_and_list/
"""
from array import array
arr = array('u', 'asdf')
print(arr)
print(arr[0])
print(arr[1])
class Array(object):
def __init__(self, size=32):
self._size = size
self._items = [None] * size
def __getitem__(self, index):
return self._items[index]
def __setitem__(self, index, value):
self._items[index] = value
def __len__(self):
return self._size
def clear(self, value=None):
for i in range(len(self._items)):
self._items[i] = value
def __iter__(self):
for item in self._items:
yield item
def test_array():
size = 10
a = Array(size)
a[0] = 1
assert a[0] == 1
a.clear()
assert a[0] is None