【发布时间】:2019-05-13 21:49:25
【问题描述】:
这是我的第一篇文章。
我有一段代码正在尝试对其进行测试。
有一个非常简单的解决方法可以让这段代码运行,那就是将整数放在引号中。
我想要完成的是拥有一个在字典中使用字符串和整数的字典。这是我的带扫描功能的字典。
lexicon = {
'1234': 'number',
3: 'number',
91234: 'number'
}
def scan(sentence):
results = []
words = sentence.split()
for word in words:
word_type = lexicon.get(word)
results.append((word_type, word))
return results
上面的代码被导入到我的包含这段代码的测试文件中。
from nose.tools import *
from ex48 import lexicon
def test_numbers():
assert_equal(lexicon.scan('1234'), [('number', '1234')])
result = lexicon.scan('3 91234')
assert_equal(result, [('number', 3),
('number', 91234)])
“1234”部分运行良好。
但是,代码中是否有可以使用 int() 的位置,以便在“3 91234”上运行 split() 时,它将返回两个整数并正确使用我的词典来回调适当的值?
感谢您的帮助!
【问题讨论】:
标签: python string dictionary integer nose