Python,乘法的SUM

时间:2010-12-10 10:30:49

标签: python text sum

我的代码:

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)

我的输出现在是:

e 00 2
i 010 3
h 011 3
t 10 2
a 11 2

我需要:

输出与现在相同,但均匀 SUM = 2 * 0.124167 + 3 * 0.0969225 +3 * 0.0820011 + 2 * 0.0768052 + 2 * 0.0368052 ... =?; 在这种情况下,SUM = 1.0123256;

第一个数字来自 len(前缀),第二个数字来自 exampleData

任何解决方案?



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.0768052 , '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)

这给了我错误

1 个答案:

答案 0 :(得分:0)

我想我知道你在追求什么。首先,我发现将频率表转换为字典很有用,因此:

freqs = dict([(b,a) for (a,b) in exampleData])

然后你可以将它传递给打印出树遍历的函数。我修改了你的函数以使用这个频率数据并跟踪总和:

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)

然后你可以在主函数中这样调用它:

huffTree = makeHuffTree(exampleData)
tot = printHuffTree2(freqs, huffTree)
print 'Sum = ', tot

这给出了0.9470124的总和,我认为根据您的示例数据,这是正确的。

完整的代码变为:

#!/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