Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: .append(0)

Tags:

python

I would like to ask what the following does in Python. It was taken from http://danieljlewis.org/files/2010/06/Jenks.pdf

I have entered comments telling what I think is happening there.

# Seems to be a function that returns a float vector
# dataList seems to be a vector of flat. 
# numClass seems to an int

def getJenksBreaks( dataList, numClass ):

    # dataList seems to be a vector of float. "Sort" seems to sort it ascendingly
    dataList.sort() 

    # create a 1-dimensional vector 
    mat1 = []

    # "in range" seems to be something like "for i = 0 to len(dataList)+1)
    for i in range(0,len(dataList)+1):

        # create a 1-dimensional-vector?
        temp = []
        for j in range(0,numClass+1):

            # append a zero to the vector?
            temp.append(0)

        # append the vector to a vector??
        mat1.append(temp)

(...)

I am a little confused because in the pdf there are no explicit variable declarations. However I think and hope I could guess the variables.

like image 916
tmighty Avatar asked Oct 17 '13 16:10

tmighty


1 Answers

Yes, the method append() adds elements to the end of the list. I think your interpretation of the code is correct.

But note the following:

x =[1,2,3,4]
x.append(5)
print(x)
    [1, 2, 3, 4, 5]

while

x.append([6,7])
print(x)
    [1, 2, 3, 4, 5, [6, 7]]

If you want something like

    [1, 2, 3, 4, 5, 6, 7]

you may use extend()

x.extend([6,7])
print(x)
    [1, 2, 3, 4, 5, 6, 7]
like image 70
PhillipD Avatar answered Oct 24 '22 15:10

PhillipD