473,396 Members | 1,789 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Heapify, bad syntax

12
With this code I'm trying to create a huffman encoding tree, to compress text. But heapify just won't work for me :(

Expand|Select|Wrap|Line Numbers
  1. def create_tree(dict):
  2.     listofNodes = []
  3.     for element in dict.items():
  4.         newNode = Node(weight = element[1], item = element[0])
  5.         listofNodes.append(newNode)
  6.     listofNodes.append(Node(weight = 1, item = 'eof')
  7.     heapify(listofNodes)
  8.     while len(listofNodes) > 1 :
  9.         x = heappop(listofNodes)
  10.         y = heappop(listofNodes)
  11.         weight1 = x.weight
  12.         weight2 = y.weight
  13.         newnode = Node(weight = weight1+weight2, item = None)
  14.         heappush(listofNodes, newnode)
  15.  
  16.     return listofNodes
For some reason the line "heapify(listofNodes)" says it has bad syntax. And for the life of me I just can't figure out what it is. The rest of the function seems fine (I know I can reduce length by getting rid of variables but this is a working copy to get the ideas clear in my head), except for that one line.

I'm on windows vista if thats important, with the latest python.

Help would be appreciated and thank you.

p.s. One interesting thing is if I comment out the line "listofNodes.append(Node(weight = 1, item = 'eof')" then the syntax is fixed. But I don't know how to get both lines of code to work still.
Feb 14 '08 #1
2 1481
bvdet
2,851 Expert Mod 2GB
With this code I'm trying to create a huffman encoding tree, to compress text. But heapify just won't work for me :(

Expand|Select|Wrap|Line Numbers
  1. def create_tree(dict):
  2.     listofNodes = []
  3.     for element in dict.items():
  4.         newNode = Node(weight = element[1], item = element[0])
  5.         listofNodes.append(newNode)
  6.     listofNodes.append(Node(weight = 1, item = 'eof')
  7.     heapify(listofNodes)
  8.     while len(listofNodes) > 1 :
  9.         x = heappop(listofNodes)
  10.         y = heappop(listofNodes)
  11.         weight1 = x.weight
  12.         weight2 = y.weight
  13.         newnode = Node(weight = weight1+weight2, item = None)
  14.         heappush(listofNodes, newnode)
  15.  
  16.     return listofNodes
For some reason the line "heapify(listofNodes)" says it has bad syntax. And for the life of me I just can't figure out what it is. The rest of the function seems fine (I know I can reduce length by getting rid of variables but this is a working copy to get the ideas clear in my head), except for that one line.

I'm on windows vista if thats important, with the latest python.

Help would be appreciated and thank you.

p.s. One interesting thing is if I comment out the line "listofNodes.append(Node(weight = 1, item = 'eof')" then the syntax is fixed. But I don't know how to get both lines of code to work still.
Try adding a closed parenthesis.
Expand|Select|Wrap|Line Numbers
  1. listofNodes.append(Node(weight = 1, item = 'eof'))
Feb 14 '08 #2
Traclo
12
Well this is embarrassing.
Feb 14 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
22
by: Tuang | last post by:
I'm checking out Python as a candidate for replacing Perl as my "Swiss Army knife" tool. The longer I can remember the syntax for performing a task, the more likely I am to use it on the spot if...
14
by: Sandy Norton | last post by:
If we are going to be stuck with @decorators for 2.4, then how about using blocks and indentation to elminate repetition and increase readability: Example 1 --------- class Klass: def...
8
by: Scott David Daniels | last post by:
I am sorry, but in the Python 2.4 description of "heapify", I find the description of "Transform list x into a heap, in-place, in linear time," unbelievable. I understand the hand-wave that makes...
16
by: George Sakkis | last post by:
I'm sure there must have been a past thread about this topic but I don't know how to find it: How about extending the "for <X> in" syntax so that X can include default arguments ? This would be very...
23
by: Carter Smith | last post by:
http://www.icarusindie.com/Literature/ebooks/ Rather than advocating wasting money on expensive books for beginners, here's my collection of ebooks that have been made freely available on-line...
19
by: Nicolas Fleury | last post by:
Hi everyone, I would to know what do you think of this PEP. Any comment welcomed (even about English mistakes). PEP: XXX Title: Specialization Syntax Version: $Revision: 1.10 $...
3
by: Manuel | last post by:
I'm trying to compile glut 3.7.6 (dowbloaded from official site)using devc++. So I've imported the glut32.dsp into devc++, included manually some headers, and start to compile. It return a very...
5
by: Dongsheng Ruan | last post by:
I want to turn an Array into a heap, but my code just doesn't work: no change after execution. A= m=len(A)-1 for i in range(m,1): t=(i-1)/2
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.