【问题标题】:Python - Accesing a list from another class methodPython - 从另一个类方法访问列表
【发布时间】:2017-10-20 11:10:57
【问题描述】:

我对两个不同的类和同一个类的两个方法有一点问题。我有一个 B 类,它使用了 A 类中的两种方法,这似乎工作正常。 然而,问题是类 a 中的第一个方法(插入)更改了该类中的第二个方法(查找)应该使用的列表。它正在使用仍然仅以零启动的全局列表。所以我不知道如何从 insert 方法中告诉方法使用 HashMap :/ 希望有人能提供帮助,谢谢!

""" PUBLIC MEMBERS

Insert the given key (given as a string) with the given value (given as 
an integer). If the hash table already contains an entry for the given key, 
update the value of this entry with the given value.
"""

class Map:
    global m 
    m = 10000
    global HashMap 
    HashMap = []
    for i in range(m):
        HashMap.append(0)

    @classmethod
    def insert(self, key, value):

        """
        >>> Map.insert("hi", 9)
        [4,53]
        """
        self.key = key
        self.value = value


        asci = 0  
        for i in key:
            asci += ord(i)
        hashindex = (asci%m)*2
        print(hashindex)
        print(HashMap[hashindex])

        if HashMap[hashindex] == key:
           HashMap[hashindex + 1] = value 

        else:
            while HashMap[hashindex] != 0:
                hashindex = ((asci+1)%m)*2  


            HashMap[hashindex] = key
            HashMap[hashindex+1] = value



    """ Check if there exists an entry with the given key in the hash table. 
   If such an entry exists, return its associated integer value. 
   Otherwise return -1.
    """

    @classmethod
    def lookup(self, key):

        self.key = key
        ascilookup = 0
        for i in key:
            ascilookup += ord(i)

        indexlookup = (ascilookup%m)*2


        for j in HashMap:
            if HashMap[j]==key:
                return HashMap[j + 1]

            elif HashMap[j]==0:
                return "-1"

            else:
                j =((j+1)%m)*2        



if __name__ == "__main__":
    import doctest
    doctest.testmod()   

【问题讨论】:

  • 为什么要使用列表来替换哈希图?使用dictionary

标签: python class methods static


【解决方案1】:

这是一个更简单的python地图实现:

class Map:
    HashMap = {}

    def __init__(self,leng):
        for i in range(leng):
            self.HashMap[str(i)]=0

    def insert(self, key, value):
        self.HashMap[key]=value


    def lookup(self, key):
        for each in self.HashMap.iterkeys():
            if each == key:
                return self.HashMap[each]
        return None

EDIT 不使用字典,使用两个列表更容易:

class Map:
    keys = []
    values = []

    def __init__(self,leng):
        for i in range(leng):
            self.keys.append(str(i))
            self.values.append(0)

    @classmethod
    def insert(self, key, value):
        self.keys.append(key)
        self.values.append(value)

    @classmethod
    def lookup(self, key):
        for x in range(0, len(self.keys)):
            if self.keys[x] == key:
                return self.values[x]
        return None

【讨论】:

  • 谢谢,问题是,我不允许使用内部字典:/
  • 啊,这就解释了。也许使用 2 个列表?
  • 我在不使用字典的情况下添加了第二个解决方案
猜你喜欢
  • 2022-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-12
  • 1970-01-01
  • 1970-01-01
  • 2012-11-12
相关资源
最近更新 更多