Reputation: 171
I want to implement a hash table in python from a file txt. My file is something like example.txt
:
aaa.12
bbb.14
ccc.10
I can open this file in python but I don't know how to import each line in a hash table built like hash:
{'aaa':12, 'bbb':14, 'ccc':10}
ok thank you very much. another question..if i want to order the value and save in the file the items in order how can i do? i try to use this: after your code i save the values v=sorted(hash.values()) and to check it if worked i print v so the result was this: v =[10, 14, 14]. So it works. but know how can i change the orginal file example.txt and save in this order: ccc.10 aaa.12 bbb.14
Upvotes: 2
Views: 2337
Reputation: 1124858
Provided the aaa.12
, etc. items are each on a separate line:
with open('example.text') as f:
hash = {}
for line in f:
key, value = line.strip().split('.', 1)
hash[key] = int(value)
Note that you probably mean the dict
type, which is a specific kind of python mapping, that happens to use a hash table in the underlying implementation.
Upvotes: 3