With this code I'm trying to create a huffman encoding tree, to compress text. But heapify just won't work for me :( - def create_tree(dict):
-
listofNodes = []
-
for element in dict.items():
-
newNode = Node(weight = element[1], item = element[0])
-
listofNodes.append(newNode)
-
listofNodes.append(Node(weight = 1, item = 'eof')
-
heapify(listofNodes)
-
while len(listofNodes) > 1 :
-
x = heappop(listofNodes)
-
y = heappop(listofNodes)
-
weight1 = x.weight
-
weight2 = y.weight
-
newnode = Node(weight = weight1+weight2, item = None)
-
heappush(listofNodes, newnode)
-
-
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.
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 :( - def create_tree(dict):
-
listofNodes = []
-
for element in dict.items():
-
newNode = Node(weight = element[1], item = element[0])
-
listofNodes.append(newNode)
-
listofNodes.append(Node(weight = 1, item = 'eof')
-
heapify(listofNodes)
-
while len(listofNodes) > 1 :
-
x = heappop(listofNodes)
-
y = heappop(listofNodes)
-
weight1 = x.weight
-
weight2 = y.weight
-
newnode = Node(weight = weight1+weight2, item = None)
-
heappush(listofNodes, newnode)
-
-
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. - listofNodes.append(Node(weight = 1, item = 'eof'))
Well this is embarrassing.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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 $...
|
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...
|
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
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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,...
| | |