Hashtable:仅有两列的表

本文介绍了Hashtable集合的特点及其使用方法。Hashtable可以视为一个两列表格,其中一列为唯一标识符(键),另一列为对应的值。文章还提供了如何通过键来访问元素值的示例,并解释了遍历Hashtable集合的方法。

Hashtable集合和ArrayList、Stack和Queue都不一样,该集合可用看作是一个仅有两列的表格,第一列是键,存放的是唯一不可重复的值。第二列是值,随便放什么都可以。由于Hashtable这样的特征,所以这个集合不适合放置顺序的值,却可以放置些能被唯一标识的值。比如以下代码说明了这个现象:

System.Collections.Hashtable phoneBook = new System.Collections.Hashtable();

phoneBook.Add("Leo", "139");

phoneBook.Add("Shy", "138");

phoneBook.Add("Hai", "137");

phoneBook.Add("Lii", "188");

怎么取值呢?令你惊讶的是,Hashtable不直接支持通过下标取值,如果我们要访问元素,必须要知道key的值。

第一种方案直接依赖key值获取元素的值:

System.Console.WriteLine(phoneBook["Leo"]);

System.Console.WriteLine(phoneBook["Shy"]);

System.Console.WriteLine(phoneBook["Hai"]);

System.Console.WriteLine(phoneBook["Lii"]);

如果你需要遍历Hashtable中的所以元素,你需要采用如下的方案:

foreach (System.Collections.DictionaryEntry entry in phoneBook)

{

System.Console.WriteLine("phoneBook[{0}]:{1}", entry.Key, phoneBook[entry.Key]);

}

得到的结果如图3.1.29:

clip_image002

图3.1.29

通过遍历DictionaryEntry我们可以得到Hashtable的所有键,值得注意的是,遍历集合的次序和值进入集合的次序没有必然的关系。

C:\Users\1\PycharmProjects\pythonProject1\.venv\Scripts\python.exe C:\Users\1\PycharmProjects\pythonProject1\1.0.py 成功读取第一个工作,共加载 1082 条记录 成功读取工作‘男胎检测数据’,共加载 1082 条记录 自动识别GC含量列: GC含量 合并GC含量完成,有效记录数: 4644 Traceback (most recent call last): File "C:\Users\1\PycharmProjects\pythonProject1\.venv\Lib\site-packages\pandas\core\indexes\base.py", line 3812, in get_loc return self._engine.get_loc(casted_key) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "pandas/_libs/index.pyx", line 167, in pandas._libs.index.IndexEngine.get_loc File "pandas/_libs/index.pyx", line 196, in pandas._libs.index.IndexEngine.get_loc File "pandas/_libs/hashtable_class_helper.pxi", line 7088, in pandas._libs.hashtable.PyObjectHashTable.get_item File "pandas/_libs/hashtable_class_helper.pxi", line 7096, in pandas._libs.hashtable.PyObjectHashTable.get_item KeyError: 'GC含量' The above exception was the direct cause of the following exception: Traceback (most recent call last): File "C:\Users\1\PycharmProjects\pythonProject1\1.0.py", line 113, in <module> df["GC含量"] = df[gc_col].apply(convert_gc) ~~^^^^^^^^ File "C:\Users\1\PycharmProjects\pythonProject1\.venv\Lib\site-packages\pandas\core\frame.py", line 4113, in __getitem__ indexer = self.columns.get_loc(key) ^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\1\PycharmProjects\pythonProject1\.venv\Lib\site-packages\pandas\core\indexes\base.py", line 3819, in get_loc raise KeyError(key) from err KeyError: 'GC含量'
最新发布
11-02
Z:\PythonProject_9_23_RandomForestRegressor_test\.venv\Scripts\python.exe Z:\PythonProject_9_23_RandomForestRegressor_test\main.py Traceback (most recent call last): File "Z:\PythonProject_9_23_RandomForestRegressor_test\.venv\Lib\site-packages\pandas\core\indexes\base.py", line 3812, in get_loc return self._engine.get_loc(casted_key) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "pandas/_libs/index.pyx", line 167, in pandas._libs.index.IndexEngine.get_loc File "pandas/_libs/index.pyx", line 196, in pandas._libs.index.IndexEngine.get_loc File "pandas/_libs/hashtable_class_helper.pxi", line 7088, in pandas._libs.hashtable.PyObjectHashTable.get_item File "pandas/_libs/hashtable_class_helper.pxi", line 7096, in pandas._libs.hashtable.PyObjectHashTable.get_item KeyError: '预测出水浊度' The above exception was the direct cause of the following exception: Traceback (most recent call last): File "Z:\PythonProject_9_23_RandomForestRegressor_test\main.py", line 36, in <module> main() File "Z:\PythonProject_9_23_RandomForestRegressor_test\main.py", line 13, in main processor = DataProcessor('JB_water_quality_data.csv') ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "Z:\PythonProject_9_23_RandomForestRegressor_test\data_processor.py", line 9, in __init__ self.preprocess_data() File "Z:\PythonProject_9_23_RandomForestRegressor_test\data_processor.py", line 25, in preprocess_data self.df['近似率'] = (1 - abs(self.df['预测出水浊度'] - self.df['实际出水浊度']) / self.df['实际出水浊度']) * 100 ~~~~~~~^^^^^^^^^^ File "Z:\PythonProject_9_23_RandomForestRegressor_test\.venv\Lib\site-packages\pandas\core\frame.py", line 4107, in __getitem__ indexer = self.columns.get_loc(key) ^^^^^^^^^^^^^^^^^^^^^^^^^ File "Z:\PythonProject_9_23_RandomForestRegressor_test\.venv\Lib\site-packages\pandas\core\indexes\base.py", line 3819, in get_loc raise KeyError(key) from err KeyError: '预测出水浊度'
09-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值