python返回默认语句

rasmus393

嘿,我在努力学习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
凯里·哈切(Kerry Hatcher)

无需测试,只需查看代码即可:

if key == kv: 

应该是(在第25行附近)

if key == k:

基本上,kv不存在,因此它始终测试为false,并且不会在if语句下运行代码。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在python中返回“ for if in”语句

来自分类Dev

Python:返回语句问题

来自分类Dev

python if语句总是返回False

来自分类Dev

Python嵌套的if语句返回错误

来自分类Dev

python if语句总是返回False

来自分类Dev

python 3中的返回语句

来自分类Dev

在Python中的“ if”语句中执行奇怪的返回语句

来自分类Dev

python`with .. as ..`语句和多个返回值

来自分类Dev

python if语句返回值错误

来自分类Dev

为什么Python不应返回False语句?

来自分类Dev

if语句未在python中返回答案

来自分类Dev

python函数中的返回语句不返回任何内容

来自分类Dev

为什么设置默认参数然后在Python中运行if语句?

来自分类Dev

返回if语句

来自分类Dev

使用return语句和默认返回最后一个值有什么区别?

来自分类Dev

无论我输入哪种大小写值,switch语句仅返回默认大小写

来自分类Dev

switch语句在Javascript每次都返回默认值,而不是案件

来自分类Dev

当未指定return语句时,为什么C ++没有默认构造返回值?

来自分类Dev

哪个Python条件返回语句最适合Python?

来自分类Dev

Python dictionary.get()默认返回不起作用

来自分类Dev

Python dictionary.get()默认返回不起作用

来自分类Dev

Python 连接表并返回未找到的 ID 的默认文本

来自分类Dev

Python递归函数执行返回语句不正确?

来自分类Dev

try语句在Python 2.7中不返回值

来自分类Dev

是否建议在python函数中使用print语句而不是返回

来自分类Dev

SQLAlchemy和Python-返回ID的upsert语句

来自分类Dev

在Python中返回语句时,如何格式化?

来自分类Dev

Python 3 If语句未返回预期内容

来自分类Dev

try语句在Python 2.7中不返回值

Related 相关文章

热门标签

归档