Data Structure and Algorithm_Sparse Array

**

1. What is a sparse array?

**
Answer: The sparse array is to store the three values ​​of the horizontal left, vertical coordinate and element size of the two-dimensional array element in one array in another array. It is more suitable when the number of elements in the array is relatively small. Therefore, sparse arrays can greatly reduce storage space.

2. The specific operation steps are, for example, in a 5 * 5 two-dimensional array, which should be converted into a sparse array, the schematic diagram is as follows:
Insert picture description here

2. Specific steps

  • 1. Traverse the two-dimensional array once to get the number sum of non-zero values ​​in the two-dimensional array, the row m and the column n of the two-dimensional array.

  • 2. Sparse array initialization: The number of rows in the sparse array is equal to the number of non-zero values ​​plus 1, ie sum + 1, and the number of columns is 3.

  • 3. Sparse array assignment: the first value of the first row represents the number of rows of the two-dimensional array, in turn represents the number of columns, and the number of non-zero values.

  • 4. After the sparse array is obtained, the sparse array can be stored in a file, and then read when needed.

  • 5. Restore the sparse array: first read the first line of the sparse array to initialize the two-dimensional array, and then assign values ​​to specific locations in turn.

**

3. What you learned when writing code:

**
How to use python to realize for loop?
Answer: Use for i in range (x, y, z): structure. In this statement, i represents the subscript of the list you need to traverse. Use x and y to control the upper and lower limits of the list, and z to control the increment of each cycle.

for i in  range(0,3,1):
	print("%d,我爱python" % i )

The execution results of this code are:
1, I love python
2, I love python
3, I love python

How to implement and use two-dimensional array with python?
Answer: There is actually no concept of array in Python, you need to use list [] to achieve.

Sparse_list = [[0 for col in range(m)] for row in range(n)]  
# 其中0为二维数组的初始值,m为列数,n为行数

Using two-dimensional arrays is no different from other languages, such as C and java. Use list names and row class coordinates to assign values.

How to use file operations to store sparse arrays?
Answer: Use the open function to open the file according to the file name and operation permission characters, and assign a value to a file object, and use the file object's writelines () method to write the file.
Note: Remember to close the file at the end of writing.

#将稀疏数组存放到文件中
fo = open("output.txt","w")
for i in range(len(sparse_list)):
    fo.writelines("%3d%3d%3d\n"%(sparse_list[i][0],sparse_list[i][1],sparse_list[i][2]))
fo.close()
print("储存成功!")

How to revert to a sparse array from the file
A: Basically, it is no different from the write operation, but during the reading process, you need to use a slicing operation to cut each line into specific data, and use the eval () function to convert the characters The string is converted into numbers and stored in a sparse array.

For those unfamiliar with slicing and eval () functions, you can Baidu, and I will not expand here.

#从文件中读取并打印出來,得到稀疏数组2
sparse_list2 = [[0 for col in range(3)] for row in range(sum+1)]
print("读取的文件为---------")
count = 0
fi = open("output.txt","r")
for line in fi:
    print(line)
    sparse_list2[count][0]=eval(line[0:3])
    sparse_list2[count][1]=eval(line[3:6])
    sparse_list2[count][2]=eval(line[6:9])
    count = count + 1
fi.close()
Published 27 original articles · praised 2 · visits 680

Guess you like

Origin blog.csdn.net/qq_44273739/article/details/104746060