Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23189

Why is map function not effective?

$
0
0

I am not struck or anything, but I feel this weird. Below is the code snippet I have worked on:

from hashClass import HashTablea = HashTable(11)input_value = list((54,26,93,17,77,31,44,55,20))map(lambda x: a.put(x,x),input_value))print(a.data)

I have created my own hash table class. It has a method called put which accepts key-value pair and "HashTable.data" attribute displays all the values in the table.

It works completely fine when I use put method with normal key-value pairs. I know that the solution can be implemented using:

for i in input_value: a.putt(i,i)

But I would like to know why the map function is not effective? When I try to map every input_value with "put" method, it has to add the value to the instance if I am not wrong. My reason is I may not be using the mapped value but syntactically when I am mapping, it is supposed to update the instance variable.

Below is the hash Class I have created for reference.

class HashTable(object):    def __init__(self,size):        self.size = size        self.slots = self.size*[None]        self.data = self.size*[None]        self.values = 0    def put(self,key,value):        hashValue = self.hashFunction(key)        if self.slots[hashValue] == None:            self.slots[hashValue] = key            self.data[hashValue] = value            self.values += 1        elif self.slots[hashValue] == key:            self.data[hashValue] = value        else:            hashValue = self.reHash(hashValue)            while self.slots[hashValue] != None and self.slots[hashValue] != key:                hashValue = self.reHash(hashValue)            self.slots[hashValue] = key            self.data[hashValue] = value            self.values += 1    def reHash(self,oldValue):        return (oldValue+1)%self.size    def __len__(self):        return self.values    def get(self,key):        hashValue = self.hashFunction(key)        if self.slots[hashValue] == None:            return "No Value associated"        elif self.slots[hashValue] == key:            return self.data[hashValue]    def hashFunction(self,key):        return key%self.size

Viewing all articles
Browse latest Browse all 23189

Trending Articles