我有一个在构造函数中有一个列表的类
class FCM:
def __init__(self, input_table, attributes, num_centroids, m, start_indep, pos_of_y):
self.xs = # it's a list of lists
self.xs
是下面传递给函数的列表列表。我制作了一份副本,因为我想确定xs
因我的功能而没有改变。
from copy import copy
xs_copy=[]
xs_copy=copy(self.xs)
self.current_centroids=self.get_initial_centroids(xs_copy)
这是功能:
def get_initial_centroids(self, these_xs):
print str(self.xs[0])
for i in range(len(these_xs)):
these_xs[i].append(self.cluster_distance(these_xs[i][self.start_indep:], avg_centroid))
print str(self.xs[0]) # Why is my class list affected???????
我想了解的是,为什么self.xs
会受到此函数的影响,因为我将其副本传递给了我的函数。
对于那些不仅仅对要点感兴趣的人:
from __future__ import division
import MySQLdb
class FCM:
def __init__(self, input_table, attributes, num_centroids, m, start_indep, pos_of_y):
self.input_table=input_table
self.db=MySQLdb.connect()
self.cursor=self.db.cursor()
self.cursor.execute("select " + ",".join(attributes) + " from " + input_table)
ys=[]
for record in self.cursor.fetchall():
ys.append([int(r) if type(r) is long else r for r in list(record)]) # some of the data is of type long and should be of type integer
self.xs=ys[:]
self.num_centroids=num_centroids
self.m=m
self.start_indep=start_indep
self.pos_of_y=pos_of_y
from copy import copy
xs_copy=[]
xs_copy=copy(self.xs)
self.current_centroids=self.get_initial_centroids(xs_copy)
print "Initial centroids are " + str(self.current_centroids)
def get_initial_centroids(self, these_xs):
running_total=these_xs[0][self.start_indep:]
cnt=0
for this_x in these_xs[1:]:
running_total = self.vector_add(running_total, this_x[self.start_indep:])
cnt += 1
avg_centroid=self.scalar_mult(running_total, 1/cnt)
print str(self.xs[0]) # this is the same as in the constructor
for i in range(len(these_xs)):
these_xs[i].append(self.cluster_distance(these_xs[i][self.start_indep:], avg_centroid))
print str(self.xs[0]) # but now self.xs has changed