The trap of the clear method of list in python: [] and clear()

About the trap of clear() and clear()

Today, I encountered a problem about list emptying when reading data with python. The solution was not effective for a long time. Finally, I found out when debugging at breakpoints that the original problem lies in the use of [] assignment and clear() function. , It also shows that there are subtle differences in the use of the two methods.

Problem overview

I want to read data from a text file. The data style of the text is:
Insert picture description here
each row represents a sample data, and the focus is on the style of each sample data. For example, the first row has 3 fields, and each field is separated by the tab key. Separate each field with a space character, and the saved data type in python should be [[[1573,1730,8392,0,689,7,2702],[96,1573,1730,8392],[[ 1]]]
The data of each subsequent row is also inserted in this form, and the final form is: the Insert picture description here
presentation form is a 3-dimensional array.

The trap

In the code, the usual idea is adopted, and the append is kept until the largest 3-dimensional array is generated.
In the initial writing, for the first two layers of arrays, after each append, the clear() function is used to clear it to prevent the last data from being saved when appending the next line of data, causing data redundancy And the wrong form, the code is as follows:

c_nums1=[]
c_nums2=[]
temp_1=[]
feats=[]
with open("train.txt") as f:
    for i in range(2):
        line=f.readline()
        temp = line.strip().split('\t')
        c_1 = temp[0].split(' ')
        c_2 = temp[1].split(' ')
        for i in c_1:
            c_nums1.append(float(i))
        for j in c_2:
            c_nums2.append(float(j))
        temp_1.append(c_nums1)
        temp_1.append(c_nums2)
        feats.append(temp_1)
        c_nums1.clear()
        c_nums2.clear()
        temp_1.clear()
f.close()
print(feats)

In the test sample, only the first two lines were taken as the test. The results are as follows: The
Insert picture description here
output results are two empty lists, which are obviously wrong, but there is no error in the above code. If you can’t find any errors, use breakpoints. Debugging for troubleshooting, and then you can clearly see that there are unexpected errors after the clear() function is executed, as follows:
Insert picture description here
You can see that after the feats.append(temp_1) is executed, there is a value in feats, but in Later, when executing the clearing of the first two layers of arrays, it was found that the value of feats was also cleared.
Insert picture description here
So here is speculation that the clear() function is like a pointer in c. When clearing this space, it will call this space before The values ​​are all cleared, as if they are all connected together. If the clear() function is changed to [] assignment, there is no such problem, everything can be output normally:
Insert picture description here
this problem is not big, but it is worth noting that the code is also a reminder here You must pay attention to these in the future.

Guess you like

Origin blog.csdn.net/jiljdlawjdlada/article/details/105411352