设为首页收藏本站

Crossin的编程教室

 找回密码
 立即加入
查看: 21610|回复: 4
打印 上一主题 下一主题

提问帖

[复制链接]

6

主题

0

好友

166

积分

注册会员

Rank: 2

跳转到指定楼层
楼主
发表于 2019-1-14 15:56:30 |只看该作者 |倒序浏览
  1. def delete(aMap, key):
  2.     """Deletes the given key from the Map."""
  3.     bucket = get_bucket(aMap, key)
  4.        
  5.     for i in xrange(len(bucket)):
  6.         k, v =bucket[i]
  7.         if key == k:
  8.             del bucket[i]
  9.             break

  10. SyntaxError: invalid syntax
复制代码
请问为什么会语法错误,按着笨办法学python打的

微信图片_20190114155500.png (6.97 KB, 下载次数: 487)

微信图片_20190114155500.png

回复

使用道具 举报

174

主题

45

好友

11万

积分

管理员

Rank: 9Rank: 9Rank: 9

沙发
发表于 2019-1-14 21:36:10 |只看该作者
没看出来。
请附上完整的报错信息(不仅是错误名称,还包括前面的信息)
以及完整的代码
#==== Crossin的编程教室 ====#
微信ID:crossincode
网站:http://crossincode.com
回复

使用道具 举报

6

主题

0

好友

166

积分

注册会员

Rank: 2

板凳
发表于 2019-1-15 18:39:37 |只看该作者
crossin先生 发表于 2019-1-14 21:36
没看出来。
请附上完整的报错信息(不仅是错误名称,还包括前面的信息)
以及完整的代码 ...
  1. def new(num_buckets=256):
  2.     """Initializes a Map with the given number of buckets."""
  3.     aMap = []
  4.     for i in range(0, num_buckets):
  5.         aMap.append([])
  6.     return aMap

  7. def hash_key(aMap, key):
  8.     """Given a key this will create a number and then convert it to
  9.         an index for the aMap's buckets."""
  10.     return hash(key) % len(aMap)

  11. def get_bucket(aMap, key):
  12.     """Given a key, find the bucket where it would go."""
  13.     bucket_id = hash_key(aMap, key)
  14.     return aMap[bucket_id]

  15. def get_slot(aMap, key, default=None):
  16.     """
  17.         Returns the indes, key, and value of a slot found in a bucket.
  18.         Returns -1, key, and default (None if not set) when not found.
  19.         """
  20.        
  21.     bucket = get_bucket(aMap, key)
  22.        
  23.     for i, kv in enumerate(bucket):
  24.         k, v = kv
  25.         if key == k:
  26.             return i, k, v
  27.        
  28.     return -1, key, default

  29. def get(aMap, key, default=None):
  30.     """Gets the value in a bucket for the given key, or the default."""
  31.     i, k, v = get_slot(aMap, key, default=default)
  32.     return value

  33. def set(aMap, key, value):
  34.     """Sets the key to the value, replacing any existing value."""
  35.     bucket = get_bucket(aMap, key)
  36.     i, k, v = get_slot(aMap, key)
  37.        
  38.     if i >= 0:
  39.         # the key exists, replace it
  40.         bucket[i] = (key, value)
  41.     else:
  42.         # the key does not, append to create it
  43.                 bucket.append((key,value())

  44. def delete(aMap, key):
  45.     """Deletes the given key from the Map."""
  46.     bucket = get_bucket(aMap, key)
  47.        
  48.     for i in xrange(len(bucket)):
  49.         k, v =bucket[i]
  50.         if key == k:
  51.             del bucket[i]
  52.             break

  53. def list(aMap):
  54.     """Prints out what's in the Map."""
  55.     for bucket in aMap:
  56.         if bucket:
  57.             for k, v in bucket:
  58.                 print k,v
复制代码
  1. import hashmap

  2. # create a mapping of state to abbreviation
  3. states = {
  4.     'Oregon': 'OR',
  5.     'Florida': 'FL',
  6.         'California': 'CA',
  7.         'New Yourk': 'NY',
  8.         'Michigan': 'MI'
  9. }

  10. # create a mapping of state to abbreviation
  11. cities = {
  12.     'CA': 'San Francisco',
  13.         'MI': 'Detroit',
  14.         'FL': 'Jacksonville'
  15. }

  16. # add some more cities
  17. cities['NY'] = 'New York'
  18. cities['OR'] = 'Portland'

  19. # print out some cities
  20. print '_'*10
  21. print "NY State has: ", cities['NY']
  22. print "OR State has: ", cities['OR']

  23. # print some states
  24. print '_'*10
  25. print "Michigan's abbreviation is: ", states['Michigan']
  26. print "Florida's abbreviation is: ", states['Florida']

  27. # do it by using the state then cities dict
  28. print '_' * 10
  29. print "Michigan has: ", cities[states['Michigan']]
  30. print "Florida has: ", cities[states['Florida']]

  31. # print every state abbreviation
  32. print '_' * 10
  33. for state, abbrev in states.items():
  34.     print "%s is abbreviated %s" % (state, abbrev)

  35. # print every city in state
  36. print '_' * 10
  37. for abbrev, city in cities.items():
  38.     print "%s has the city %s" % (abbrev, city)

  39. # now do both at the same time
  40. print '_' * 10
  41. for state, abbrev in states.items():
  42.     print "%s state is abbreviated %s and has city %s" % (
  43.             state, abbrev, cities[abbrev])

  44. print '_' * 10
  45. # safely get a abbreviation by state that might not be there\
  46. state = states.get('Texas')

  47. if not state:
  48.     print "Sorry, no Texas."

  49. # get a city with a default value       
  50. city = cities.get('TX', 'Does Not Exit')
  51. 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
回复

使用道具 举报

6

主题

2

好友

654

积分

实习版主

Rank: 7Rank: 7Rank: 7

地板
发表于 2019-1-16 11:14:21 |只看该作者
东东哥 发表于 2019-1-15 18:39
File "ex39.py", line 1, in
    import hashmap
  File "G:\PY2\Notep\hashmap.py", line 50

你贴的第一段代码hashmap.py中第48行
        bucket.append((key,value())
结尾漏了个),加上之后应该就好了
回复

使用道具 举报

6

主题

0

好友

166

积分

注册会员

Rank: 2

5#
发表于 2019-1-16 14:21:00 |只看该作者
TED 发表于 2019-1-16 11:14
你贴的第一段代码hashmap.py中第48行
        bucket.append((key,value())
结尾漏了个),加上之后应该就好 ...

谢谢,可以了。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即加入

QQ|手机版|Archiver|Crossin的编程教室 ( 苏ICP备15063769号  

GMT+8, 2024-11-22 05:09 , Processed in 0.025483 second(s), 24 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部