代码之家  ›  专栏  ›  技术社区  ›  rasmus393

python返回默认语句

  •  0
  • rasmus393  · 技术社区  · 9 年前

    嘿,我做了一件艰难的学习python的事情,我坚持做练习39。我有以下代码,我已经经历了很多次,试图找到我的错误 首先,我有一个模块,稍后将其导入到主项目中,如下所示

    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 in to an index for the aMaps 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):
        """retuirn the index , key , nad value of a slot found i an bucket.
        returns -1 key and feult none if not when not found
        """
        bucket = get_bucket(aMap, key)
    
        for i, kv in enumerate(bucket):
            k, v = kv
            if key == kv: 
                return i, k, v
    
        return -1, key, default
    
    def get(aMap, key, default=None):
        """gives the value in a bucket for the given or the default"""
        i, k, v = get_slot(aMap, key, default=default)
        return v
    
    def set(aMap, key, value):
        """sewts thje key to the valuie replacing any existing valuie"""
        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):
        """deltes 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 whats 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 abrreviation 
    states = hashmap.new()
    hashmap.set(states, 'oregon', 'or')
    hashmap.set(states, 'florida', 'fl')
    hashmap.set(states, "california", "ca")
    hashmap.set(states, "new york", "ny")
    hashmap.set(states, "michigan", "mi")
    
    #create a basic set of states and some cities in them 
    cities = hashmap.new()
    hashmap.set(cities, "ca", "sanfrancisco")
    hashmap.set(cities, "mi", "detroit")
    hashmap.set(cities, "fl", "jacksonville")
    
    #add some more cities
    hashmap.set(cities, "ny", "new york")
    hashmap.set(cities, "or", "portland")
    
    #print some cities
    print "_" * 10
    print "ny state has: %s" % hashmap.get(cities, "ny")
    print "or state has: %s" % hashmap.get(cities, "or")
    
    #print some states
    print "-" * 10 
    print "michigans abbreviation is %s" % hashmap.get(states, "michigan")
    print "floridas abbreviation is: %s" % hashmap.get(states, "florida")
    
    # do it by suing the then cities dict
    print "-" * 10
    print "michigan has: %s" % hashmap.get(cities, hashmap.get(states, "michigan"))
    print "florida has: %s" % hashmap.get(cities, hashmap.get(states, "florida"))
    
    #print every state abbreviation
    print "-" * 10
    hashmap.list(states)
    
    # print every city in state
    print "-" * 10
    
    hashmap.list(cities)
    
    print "-" * 10
    
    state = hashmap.get(states, "texas")
    
    if not state:
        print "sorry no texas."
    
    #default values using II= with nil result   #can you do this on one line
    city = hashmap.get(cities, "tx", "does not exist")
    print "the city for the state tx is: %s" % city
    

    由于某种原因,我无法找到它在运行时返回

    ny state has: None
    or state has: None
    ----------
    michigans abbreviation is None
    floridas abbreviation is: None
    ----------
    michigan has: None
    florida has: None
    
    1 回复  |  直到 9 年前
        1
  •  0
  •   Kerry Hatcher    9 年前

    没有外部测试,只需查看代码:

    if key == kv: 
    

    应该是(第25行附近)

    if key == k:
    

    基本上kv不存在,所以它总是测试false,并且不在if语句下运行代码。