我正在尝试在python
中实施Trie
字典。我正在使用Trie
来实现它。我有这个代码,从字典中的单词创建import string
PHONE_LETTERS = 'abcdefghijklmnopqrstuvwxyz'
PHONE_NUMBERS = '22233344455566677778889999'
PHONE_TRANS = string.maketrans(PHONE_LETTERS, PHONE_NUMBERS)
class Node:
def __init__(self, key):
self.children = {}
self.key = key
self.values = []
def append_word(node, sequence, completeword):
if not sequence:
return
key = sequence[0]
try:
child = node.children[key]
except KeyError:
child = Node(key)
node.children[key] = child
if len(sequence) == 1:
child.values.append(completeword)
else:
append_word(child, sequence[1:], completeword)
def lookup(node, sequence=None):
if sequence:
# there are still numbers in the sequence: follow them in the trie
try:
child = node.children[sequence[0]]
return lookup(child, sequence[1:])
except KeyError:
return []
else:
# the sequence is empty: explore the trie using a DFS
result = node.values[:]
for child in node.children.values():
result.extend(lookup(child))
return result
def main():
root = Node(None)
words = ['hello','hess','home','abhi','busy','disturb']
for wlist in words:
print wlist
map(lambda l: append_word(root, l.strip().translate(PHONE_TRANS), l.strip()), wlist)
words = sorted(lookup(root, '43'))
print "Words: %s" % words
if __name__ == '__main__':
main()
,然后查找模式
[hello,hess]
现在当我运行这个时,我应该<!doctype html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
</head>
<body>
<button id="btn_not">Click me to get notified</button>
<script type="text/javascript">
$(document).ready(function () {
if (Notification.permission !== "granted") {
Notification.requestPermission();
}
$("#btn_not").click(function () {
createNotification();
})
})
function createNotification() {
var notification = new Notification("Hi there!");
}
</script>
</body>
</html>
输出正确吗?但我得到一个空列表。我在这里犯了什么错误?
答案 0 :(得分:1)
当你map
append
你的wlist
函数真的有意将它映射到整个 translate
字符串上时?
如果您拨打wlist
字符串上的hello
hess
home
abhi
busy
disturb
Words: ['hello', 'hess']
而不是每封信件,请致电:
map
您所要做的就是删除import string
PHONE_LETTERS = 'abcdefghijklmnopqrstuvwxyz'
PHONE_NUMBERS = '22233344455566677778889999'
PHONE_TRANS = string.maketrans(PHONE_LETTERS, PHONE_NUMBERS)
class Node:
def __init__(self, key):
self.children = {}
self.key = key
self.values = []
def append_word(node, sequence, completeword):
if not sequence:
return
key = sequence[0]
try:
child = node.children[key]
except KeyError:
child = Node(key)
node.children[key] = child
if len(sequence) == 1:
child.values.append(completeword)
else:
append_word(child, sequence[1:], completeword)
def lookup(node, sequence=None):
if sequence:
# there are still numbers in the sequence: follow them in the trie
try:
child = node.children[sequence[0]]
return lookup(child, sequence[1:])
except KeyError:
return []
else:
# the sequence is empty: explore the trie using a DFS
result = node.values[:]
for child in node.children.values():
result.extend(lookup(child))
return result
def main():
root = Node(None)
words = ['hello','hess','home','abhi','busy','disturb']
for wlist in words:
print wlist
# XXX here, you shouldn't be mapping the `translate` call onto the entire string
append_word(root, wlist.strip().translate(PHONE_TRANS), wlist)
words = sorted(lookup(root, '43'))
print "Words: %s" % words
if __name__ == '__main__':
main()
电话:
Private Sub CommandButton1_Click()
Dim ts As Control
For Each ts In Me.Controls
If ts.Name = "TextBox1" Then
'do
ElseIf ts.Name = "TextBox2" Then
'do
ElseIf ts.Name = "TextBox3" Then
'do
ElseIf ts.Name = "TextBox4" Then
'do
End If
Next
End Sub