Python, SUM of multiply
My code:
import heapq
def makeHuffTree(symbolTupleList):
trees = list(symbolTupleList)
heapq.heapify(trees)
while len(trees) > 1:
childR, childL = heapq.heappop(trees), heapq.heappop(trees)
parent = (childL[0] + childR[0], childL, childR)
heapq.heappush(trees, parent)
return trees[0]
def printHuffTree(huffTree, prefix = ''):
if len(huffTree) == 2:
print huffTree[1], prefix, len(prefix) <--------------------------
else:
printHuffTree(huffTree[1], prefix + '0')
printHuffTree(huffTree[2], prefix + '1')
exampleData = [ <-------------------------------
(0.124167 , 'e'),
(0.0969225 , 't'),
(0.0820011 , 'a'),
(0.0768052 , 'i'),
(0.0368052 , 'h')
]
""" some test code
exampleData[i] = exampleData[i] + (len(prefix),)
sum(i[1]*i[0] for i in exampleData) <-this is wrong...
"""
if __name__ == '__main__':
huffTree = makeHuffTree(exampleData)
printHuffTree(huffTree)
My output now is:
e 00 2
i 010 3
h 011 3
t 10 2
a 11 2
I need:
Output same as now, but even SUM= 2*0.124167+ 3*0.0969225 +3 *0.0820011+ 2*0.0768052+2*0.0368052...=?; soo in this case, SUM=1.0123256;
first number is from len(prefix) and second number is from exampleData
Any solution?
EDIT2:
import heapq
def makeHuffTree(symbolTupleList):
trees = list(symbolTupleList)
heapq.heapify(trees)
while len(trees) > 1:
childR, childL = heapq.heappop(trees), heapq.heappop(trees)
parent = (childL[0] + childR[0], childL, childR)
heapq.heappush(trees, parent)
return trees[0]
def printHuffTree2(freqs, huffTree, prefix = ''):
if len(huffTree) == 2:
letter = huffTree[1]
val = len(prefix)*freqs[letter]
print '%s: %s\t%u * %f = %f' % \
(huffTree[1], prefix, len(prefix), freqs[letter], val)
return val
else:
lhs = printHuffTree2(freqs, huffTree[1], prefix + '0')
rhs = printHuffTree2(freqs, huffTree[2], prefix + '1')
return (lhs+rhs)
exampleData = [
(0.124167 , 'e'),
(0.0969225 , 't'),
(0.0820011 , 'a'),
(0.07开发者_运维百科68052 , 'i'),
(0.0368052 , 'h')
]
freqs = dict([(b,a) for (a,b) in exampleData])
"""
exampleData[i] = exampleData[i] + (len(prefix),)
sum(i[1]*i[0] for i in exampleData)
"""
if __name__ == '__main__':
huffTree = makeHuffTree(exampleData)
printHuffTree2(huffTree)
This give me error's
I think I see what you're after. First, I found it useful to convert your frequency table to a dictionary, thus:
freqs = dict([(b,a) for (a,b) in exampleData])
Then you can pass this to the function that prints out the tree traversal. I modified your function to use this frequency data and track the sum:
def printHuffTree2(freqs, huffTree, prefix = ''):
if len(huffTree) == 2:
letter = huffTree[1]
val = len(prefix)*freqs[letter]
print '%s: %s\t%u * %f = %f' % \
(huffTree[1], prefix, len(prefix), freqs[letter], val)
return val
else:
lhs = printHuffTree2(freqs, huffTree[1], prefix + '0')
rhs = printHuffTree2(freqs, huffTree[2], prefix + '1')
return (lhs+rhs)
Then you can just call it like this in your main function:
huffTree = makeHuffTree(exampleData)
tot = printHuffTree2(freqs, huffTree)
print 'Sum = ', tot
This gives a sum of 0.9470124 which I think is correct given your example data.
The full code becomes:
#!/usr/bin/env python
import heapq
def makeHuffTree(symbolTupleList):
trees = list(symbolTupleList)
heapq.heapify(trees)
while len(trees) > 1:
childR, childL = heapq.heappop(trees), heapq.heappop(trees)
parent = (childL[0] + childR[0], childL, childR)
heapq.heappush(trees, parent)
return trees[0]
def printHuffTree(huffTree, prefix = ''):
if len(huffTree) == 2:
print huffTree[1], prefix, len(prefix)
else:
printHuffTree(huffTree[1], prefix + '0')
printHuffTree(huffTree[2], prefix + '1')
def printHuffTree2(freqs, huffTree, prefix = ''):
if len(huffTree) == 2:
letter = huffTree[1]
val = len(prefix)*freqs[letter]
print '%s: %s\t%u * %f = %f' % \
(huffTree[1], prefix, len(prefix), freqs[letter], val)
return val
else:
lhs = printHuffTree2(freqs, huffTree[1], prefix + '0')
rhs = printHuffTree2(freqs, huffTree[2], prefix + '1')
return (lhs+rhs)
def buildHuffTree(huffTree, prefix = ''):
if len(huffTree) == 2:
return (huffTree[1], prefix, len(prefix))
else:
return (buildHuffTree(huffTree[1], prefix + '0'),
buildHuffTree(huffTree[2], prefix + '1'))
if __name__ == '__main__':
exampleData = [
(0.124167 , 'e'),
(0.0969225 , 't'),
(0.0820011 , 'a'),
(0.0768052 , 'i'),
(0.0368052 , 'h')
]
freqs = dict([(b,a) for (a,b) in exampleData])
huffTree = makeHuffTree(exampleData)
tot = printHuffTree2(freqs, huffTree)
print 'Sum = ', tot
精彩评论