【发布时间】:2021-10-20 18:56:10
【问题描述】:
我正在将包数据从 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>]]
我不明白如何在另一个帖子中提出与我的项目类似的问题,所以我现在在这里。非常感谢大家的帮助!
【问题讨论】:
-
为您的
Package类编写一个__repr__()方法,打印您想要的内容。 -
这与哈希表无关。如果您只是打印一个包含
Package对象的变量,也会遇到同样的问题。 -
为什么要实现自己的哈希表而不是只使用字典?
-
为什么它的打印方式很重要?这就是您的 o 文本在打印时的表现方式。
标签: python hashtable memory-address