82
Chapter 5
I
Hash tables
Each time someone new comes in to vote, you have to scan this giant
list to see if they’ve already voted. But there’s a better way: use a hash!
First, make a hash to keep track of the people who have voted:
>>> voted = {}
When someone new comes in to vote, check if they’re already in
the hash:
>>> value = voted.get(“tom”)
The
get
function returns the value if “tom” is in the hash table.
Otherwise, it returns
None
. You can use this to check if someone
has already voted!
Here’s the code:
voted = {}
Dostları ilə paylaş: