Reputation: 676
I have two functions i.e.funct#1 and funct#2 (Python 3.x) and both have same return value. I am a newbie to Python and have little knowledge of C/C++ pointers' concepts. Changing one variable doesn't reflect to other unless it is not "pass by reference" i.e. pointer variables.
#<funct#1>
def normalize_vector_list(vector_list):
normal_vector_list = vector_list
for idx, vector in enumerate(vector_list):
vector_normal = cosine_normalization(vector.values())
for key in vector.keys():
vector[key] = vector[key]/vector_normal
normal_vector_list[idx] = vector
return normal_vector_list
#<funct2>
def normalize_vector_list(vector_list):
normal_vector_list = vector_list
for vector in vector_list:
vector_normal = cosine_normalization(vector.values())
for key in vector.keys():
vector[key] = vector[key]/vector_normal
return normal_vector_list
By the way, another function mentioned above i.e. cosine_normalization() is:
def cosine_normalization(vector):
return math.sqrt(sum(i**2 for i in vector))
Input parameter: vector_list
vector_list is a list of dictionaries, where vector_list[0] is following
{'controversial': 3.2988353690005856, 'new': 0.8321110382957757, 'rule': 2.57246254081232, 'patenting': 27.617294602624597, 'computer-based': 5.993462549770655, 'invention': 10.075902209486438, 'put': 1.7977654932882667, 'hold': 4.981825347696424, 'due': 2.209272915852394, 'last': 0.8984861072406485, 'minute': 2.5922651681085, 'intervention': 4.607168188650764, 'poland': 10.23598762483351}
Output: The output is normalized vector_list where each value is normalized by division with squared sum of all values. For eg. normal_vector_list[121] is {'high-speed': 0.05849977431100451, 'net': 0.1026094844409006, 'connection': 0.232742384495927, 'proving': 0.060052144835127615, 'popular': 0.03800614678175985, 'ever': 0.034539324332692536, 'report': 0.026431249541766944, 'people': 0.09935938942455483, 'signed': 0.04328893168172482, 'broadband': 0.41893629209266864, 'last': 0.012408508988158977, 'three': 0.03841976800723183......}
Upvotes: 0
Views: 569
Reputation: 674
By passing a list
or dict
to a function you are passing the actual object rather than it's copy.
You must first create a copy of the object and then operate on it.
import copy
def normalize_vector_list(vector_list):
normal_vector_list = copy.deepcopy(vector_list)
for idx, vector in enumerate(vector_list):
vector_normal = cosine_normalization(vector.values())
for key in vector.keys():
vector[key] = vector[key]/vector_normal
normal_vector_list[idx] = vector
return normal_vector_list
Upvotes: 1