如何为我的哈希表打印值而不是内存地址? Python

How to print values instead of memory addresses for my hashtable? Python

我正在将包数据从 csv 读入我的哈希表,当我去打印数据以检查它是否被正确插入时,我得到了值的内存地址。它读取 packageID 并正确打印该部分。 对于目前将整个项目格式化为一个文件,我深表歉意。我不熟悉 Python 以及这些文件如何协同工作。当我将它们拆分为 Pycharm 中的单独文件时,我不知道正在执行什么,因为它的下拉菜单一次会 运行 一个文件(我的猜测)?

import csv
import math


####################################################################################################################

class hashtable:
    # Set initial capacity of the Hashtable to 40 and sets the buckets to empty.
    def __init__(self, initial_capacity=40):
        # initialize the hash table with empty bucket list entries.
        self.table = []
        for i in range(initial_capacity):
            self.table.append([])

    # Inserts new key:value pair into desired bucket.
    def insert(self, key, item):  # does both insert and update
        # get the bucket list where this item will go.
        bucket = hash(key) % len(self.table)
        bucket_list = self.table[bucket]
        key_value = [key, item]
        bucket_list.append(key_value)
        return True

    # Searches for the package using its packageID as the key and returns it if found.
    def search(self, key):
        # get the bucket list where this key would be.
        bucket = hash(key) % len(self.table)
        bucket_list = self.table[bucket]
        # print(bucket_list)

        # search for the key in the bucket list
        for kv in bucket_list:
            # print (key_value)
            if kv[0] == key:
                return kv[1]  # value
        return None

    # Removes a package with the matching key.
    def remove(self, key):
        # get the bucket list where this item will be removed from.
        bucket = hash(key) % len(self.table)
        bucket_list = self.table[bucket]

        # remove the item from the bucket list if it is present.
        for kv in bucket_list:
            # print (key_value)
            if kv[0] == key:
                bucket_list.remove([kv[0], kv[1]])


####################################################################################################################

class Package:
    def __init__(self, ID, Address, City, State, Zip, Deadline, Weight, Notes, Status, Deliverytime):
        self.packageID = ID
        self.packageAddress = Address
        self.packageCity = City
        self.packageState = State
        self.packageZip = Zip
        self.packageDeadline = Deadline
        self.packageWeight = Weight
        self.packageNotes = Notes
        self.packageStatus = Status
        self.deliveryTime = Deliverytime


#####################################################################################################################


def loadPackageData(fileName):
    with open(fileName) as Package_List:
        packageData = csv.reader(Package_List, delimiter=',')
        for package in packageData:
            pID = int(package[0])
            pAddress = package[1]
            pCity = package[2]
            pState = package[3]
            pZip = package[4]
            pDeadline = package[5]
            pWeight = package[6]
            pNotes = package[7]
            pStatus = "At hub"
            pDeliverytime = "00:00"

            #package object
            p = Package(pID, pAddress, pCity, pState, pZip, pDeadline, pWeight, pNotes, pStatus, pDeliverytime)
            #print (p)

            # insert into the hash table
            myHash.insert(pID, p)

# Hash table instance
myHash = hashtable()

# Load packages to Hashtable
loadPackageData('packageData.csv')

print("Packages from Hashtable:")
print(myHash.table)
# Fetch data from hashtable
#for i in range (len(myHash.table)+1):
#    print("Package: {}".format(myHash.search(i+1))) # 1 to 40 sent to myHash.search()

当调用 print(myHash.table) 时,除了所有 40 个包之外,我得到的输出结构如下。

[[[40, <__main__.Package object at 0x000001E1489114C0>]], [[1, <__main__.Package object at 0x000001E148914220>]], [[2, <__main__.Package object at 0x000001E148911DF0>]], [[3, <__main__.Package object at 0x000001E148911D00>]]

我无法理解如何实现另一个 post 询问与我的项目类似的问题,所以我现在在这里。非常感谢大家的帮助!

Package class 中,您将编写一个 __repr__ 函数,该函数将 return 一个您想要的字符串。

您必须为 class 覆盖 __repr__ and __str__ 方法才能打印您需要的内容。

您可以通过其他方式添加自己的 human_readable-like 属性

class PackageA:
  def __init__(self, ID, Address, City, State, Zip, Deadline, Weight, Notes, Status, Deliverytime):
    self.packageID = ID
    self.packageAddress = Address
    self.packageCity = City
    self.packageState = State
    self.packageZip = Zip
    self.packageDeadline = Deadline
    self.packageWeight = Weight
    self.packageNotes = Notes
    self.packageStatus = Status
    self.deliveryTime = Deliverytime

class PackageB:
  def __init__(self, ID, Address, City, State, Zip, Deadline, Weight, Notes, Status, Deliverytime):
    self.packageID = ID
    self.packageAddress = Address
    self.packageCity = City
    self.packageState = State
    self.packageZip = Zip     
    self.packageDeadline = Deadline
    self.packageWeight = Weight
    self.packageNotes = Notes
    self.packageStatus = Status
    self.deliveryTime = Deliverytime

  def __str__(self):
    return f'STR is <{self.packageID} {self.packageAddress} {self.packageCity} {self.packageState} {self.packageZip} {self.packageDeadline} {self.packageWeight} {self.packageNotes} {self.packageStatus} {self.deliveryTime}>'

  def __repr__(self):
    return f'REPR is <{self.packageID} {self.packageAddress} {self.packageCity} {self.packageState} {self.packageZip} {self.packageDeadline} {self.packageWeight} {self.packageNotes} {self.packageStatus} {self.deliveryTime}>'

  @property
  def human_readable(self):
    return f'Readable representation is <{self.packageID} {self.packageAddress} {self.packageCity} {self.packageState} {self.packageZip} {self.packageDeadline} {self.packageWeight} {self.packageNotes} {self.packageStatus} {self.deliveryTime}>'

packagesA = []
packagesB = []

for i in range(0,3):
  packagesA.append(PackageA(f"ID{i}", f"Address{i}", f"City{i}", f"State{i}", f"Zip{i}", f"Deadline{i}", f"Weight{i}", f"Notes{i}", f"Status{i}", f"Deliverytime{i}"))
  packagesB.append(PackageB(f"ID{i}", f"Address{i}", f"City{i}", f"State{i}", f"Zip{i}", f"Deadline{i}", f"Weight{i}", f"Notes{i}", f"Status{i}", f"Deliverytime{i}"))


print(packagesA)
print("--")
print(packagesB)
print("--")
print([str(x) for x in packagesB])
print("--")
print([x.human_readable for x in packagesB])

输出:

[<__main__.PackageA object at 0x104ca0710>, <__main__.PackageA object at 0x104ca0a90>, <__main__.PackageA object at 0x104ca0e10>]
--
[REPR is <ID0 Address0 City0 State0 Zip0 Deadline0 Weight0 Notes0 Status0 Deliverytime0>, REPR is <ID1 Address1 City1 State1 Zip1 Deadline1 Weight1 Notes1 Status1 Deliverytime1>, REPR is <ID2 Address2 City2 State2 Zip2 Deadline2 Weight2 Notes2 Status2 Deliverytime2>]
--
['STR is <ID0 Address0 City0 State0 Zip0 Deadline0 Weight0 Notes0 Status0 Deliverytime0>', 'STR is <ID1 Address1 City1 State1 Zip1 Deadline1 Weight1 Notes1 Status1 Deliverytime1>', 'STR is <ID2 Address2 City2 State2 Zip2 Deadline2 Weight2 Notes2 Status2 Deliverytime2>']
--
['Readeable representation is <ID0 Address0 City0 State0 Zip0 Deadline0 Weight0 Notes0 Status0 Deliverytime0>', 'Readeable representation is <ID1 Address1 City1 State1 Zip1 Deadline1 Weight1 Notes1 Status1 Deliverytime1>', 'Readeable representation is <ID2 Address2 City2 State2 Zip2 Deadline2 Weight2 Notes2 Status2 Deliverytime2>']

在这种情况下,它打印 python 个对象,默认 __repr__() 打印 classname<memory location>。您可能想要编写一个自定义 __repr__(),其中 returns 您想要显示的数据的字符串。

所以在你的情况下你可能想做这样的事情:

class Package:
    def __init__(self, ID, Address, City, State, Zip, Deadline, Weight, Notes, Status, Deliverytime):
        self.packageID = ID
        self.packageAddress = Address
        self.packageCity = City
        self.packageState = State
        self.packageZip = Zip
        self.packageDeadline = Deadline
        self.packageWeight = Weight
        self.packageNotes = Notes
        self.packageStatus = Status
        self.deliveryTime = Deliverytime
    
    def __repr__(self):
        # Creates a list of lists where [value, attribute]
        attributes = [[getattr(self, attr), attr] for attr in dir(self) if not attr.startswith("__")]

        # Start result string with classname
        result = "Package("

        for attribute in attributes:
            result += f"{attribute[1]}= {attribute[0]},"
        
        result+=")"
        return result

print(Package(1,1,1,1,1,1,1,1,1,1)) # prints Package(deliveryTime= 1,packageAddress= 1,packageCity= 1,packageDeadline= 1,packageID= 1,packageNotes= 1,packageState= 1,packageStatus= 1,packageWeight= 1,packageZip= 1,)

这是一个非常快速和肮脏的实现,您可以清理得更快并完全按照您的意愿进行操作,但基本上它会遍历 class 并遍历每个非 dunder(从 [=14 开始) =]) 属性,然后为 class.

中的每个属性创建一个 classname(attribute=value,) 形式的字符串

看看你的 class 你也可以使用 dataclasses module because you have so many attributes, but since I don't know the types I can't give you a solution, or use a namedtuple。所有这些都内置了 __repr__() 功能。