- 帖子
- 20
- 精华
- 0
- 积分
- 166
- 阅读权限
- 20
- 注册时间
- 2018-10-15
- 最后登录
- 2019-4-13
|
crossin先生 发表于 2019-1-14 21:36
没看出来。
请附上完整的报错信息(不仅是错误名称,还包括前面的信息)
以及完整的代码 ... - def new(num_buckets=256):
- """Initializes a Map with the given number of buckets."""
- aMap = []
- for i in range(0, num_buckets):
- aMap.append([])
- return aMap
- def hash_key(aMap, key):
- """Given a key this will create a number and then convert it to
- an index for the aMap's buckets."""
- return hash(key) % len(aMap)
- def get_bucket(aMap, key):
- """Given a key, find the bucket where it would go."""
- bucket_id = hash_key(aMap, key)
- return aMap[bucket_id]
- def get_slot(aMap, key, default=None):
- """
- Returns the indes, key, and value of a slot found in a bucket.
- Returns -1, key, and default (None if not set) when not found.
- """
-
- bucket = get_bucket(aMap, key)
-
- for i, kv in enumerate(bucket):
- k, v = kv
- if key == k:
- return i, k, v
-
- return -1, key, default
- def get(aMap, key, default=None):
- """Gets the value in a bucket for the given key, or the default."""
- i, k, v = get_slot(aMap, key, default=default)
- return value
- def set(aMap, key, value):
- """Sets the key to the value, replacing any existing value."""
- bucket = get_bucket(aMap, key)
- i, k, v = get_slot(aMap, key)
-
- if i >= 0:
- # the key exists, replace it
- bucket[i] = (key, value)
- else:
- # the key does not, append to create it
- bucket.append((key,value())
- def delete(aMap, key):
- """Deletes the given key from the Map."""
- bucket = get_bucket(aMap, key)
-
- for i in xrange(len(bucket)):
- k, v =bucket[i]
- if key == k:
- del bucket[i]
- break
- def list(aMap):
- """Prints out what's in the Map."""
- for bucket in aMap:
- if bucket:
- for k, v in bucket:
- print k,v
复制代码- import hashmap
- # create a mapping of state to abbreviation
- states = {
- 'Oregon': 'OR',
- 'Florida': 'FL',
- 'California': 'CA',
- 'New Yourk': 'NY',
- 'Michigan': 'MI'
- }
- # create a mapping of state to abbreviation
- cities = {
- 'CA': 'San Francisco',
- 'MI': 'Detroit',
- 'FL': 'Jacksonville'
- }
- # add some more cities
- cities['NY'] = 'New York'
- cities['OR'] = 'Portland'
- # print out some cities
- print '_'*10
- print "NY State has: ", cities['NY']
- print "OR State has: ", cities['OR']
- # print some states
- print '_'*10
- print "Michigan's abbreviation is: ", states['Michigan']
- print "Florida's abbreviation is: ", states['Florida']
- # do it by using the state then cities dict
- print '_' * 10
- print "Michigan has: ", cities[states['Michigan']]
- print "Florida has: ", cities[states['Florida']]
- # print every state abbreviation
- print '_' * 10
- for state, abbrev in states.items():
- print "%s is abbreviated %s" % (state, abbrev)
- # print every city in state
- print '_' * 10
- for abbrev, city in cities.items():
- print "%s has the city %s" % (abbrev, city)
- # now do both at the same time
- print '_' * 10
- for state, abbrev in states.items():
- print "%s state is abbreviated %s and has city %s" % (
- state, abbrev, cities[abbrev])
- print '_' * 10
- # safely get a abbreviation by state that might not be there\
- state = states.get('Texas')
- if not state:
- print "Sorry, no Texas."
- # get a city with a default value
- city = cities.get('TX', 'Does Not Exit')
- print "The city for the state 'TX' is: %s" % city
复制代码 File "ex39.py", line 1, in <module>
import hashmap
File "G:\PY2\Notep\hashmap.py", line 50
def delete(aMap, key):
^
SyntaxError: invalid syntax
|
|