hey everybody, this is my first time posting here. i'm pretty new to
python and programming in general (as you'll soon work out for
yourselves...)
i'm trying to code a version of a selection sort and the heart of the
code is as follows (no_lines is simply the number of items to be
sorted, read out of an input file):
for j in range(0, no_lines):
k = 0
while k < no_lines:
sorted_check = 0
if list_initial[k] < list_initial[k+1]:
temp_str = list_initial[k]
elif list_initial[k] == list_initial[k+1]:
temp_str = list_initial[k]
elif list_initial[k] list_initial[k+1]:
temp_str = list_initial[k+1]
sorted_check = 1
k += 1
list_initial.re move(temp_str)
list_final.appe nd(temp_str)
no_lines -= 1
if sorted_check == 0:
break
problem is, i keep getting a "list index out of range" error. i've had
this problem before in different contexts with lists in loops.
i thought i had it cracked when it occurred to me that i needed to
decrement no_lines to take into account that list_initial was shrinking
as i deleted sorted items, but i still got the same error. it's
probably something trivial, but i can't seem to get round it by using
while loops, and i had the same problem with cmp(), hence the explicit
comparison above, which still doesn't work. can anyone help a beginner
out with this?
many thanks in advance,
sam lynas 8 28462
actually, that little bit of code i wrote is obscenely wrong anyway, so
please don't bother analyzing the flow.
any insight into the "list index out of range" error would still be
welcome, though.
In <11************ **********@h48g 2000cwc.googleg roups.com>, sam wrote:
i'm trying to code a version of a selection sort and the heart of the
code is as follows (no_lines is simply the number of items to be
sorted, read out of an input file):
for j in range(0, no_lines):
k = 0
while k < no_lines:
sorted_check = 0
if list_initial[k] < list_initial[k+1]:
temp_str = list_initial[k]
elif list_initial[k] == list_initial[k+1]:
temp_str = list_initial[k]
elif list_initial[k] list_initial[k+1]:
temp_str = list_initial[k+1]
sorted_check = 1
k += 1
list_initial.re move(temp_str)
list_final.appe nd(temp_str)
no_lines -= 1
if sorted_check == 0:
break
problem is, i keep getting a "list index out of range" error. i've had
this problem before in different contexts with lists in loops.
i thought i had it cracked when it occurred to me that i needed to
decrement no_lines to take into account that list_initial was shrinking
as i deleted sorted items, but i still got the same error. it's
probably something trivial, but i can't seem to get round it by using
while loops, and i had the same problem with cmp(), hence the explicit
comparison above, which still doesn't work. can anyone help a beginner
out with this?
It has nothing to do with `cmp()` vs. explicit testing but with indexing
the `k+1` element. Let's assume `no_lines` is 10 then the elements have
the indexes 0 to 9. Within the while loop `k` is incremented and the loop
body is executed as long as `k < 10`. When `k == 9` you try to access the
element at index `k+1`, but there is no element at index 10. So you get
the `IndexError`.
Ciao,
Marc 'BlackJack' Rintsch
yes, yes, of course, thank you. not sure what i thought i was doing
there.
i'll see if i can get it running now...
for what it's worth. and it is approx. five times quicker than the
bubblesort i wrote to begin with on a 286-word highly unordered list,
so i wasn't wasting my time after all...
_______________ _______________ _______________ _______________ __________
import time
file_input = open('wordlist. txt', 'r')
list_initial = []
no_lines = 0
for line in file_input:
list_initial.ap pend(line)
no_lines += 1
no_lines_2 = no_lines
print list_initial
file_input.clos e()
raw_input('pres s enter to sort: ')
list_final = []
temp_str = ""
time1 = time.clock()
for j in range(no_lines_ 2):
temp_str = 'zzzzzzzzzzzzzz zzzz'
for k in range(no_lines) :
if temp_str list_initial[k]:
temp_str = list_initial[k]
list_initial.re move(temp_str)
list_final.appe nd(temp_str)
no_lines -= 1
time2 = time.clock()
final_time = time2 - time1
print list_final
print
print 'number of seconds for sort list: ', final_time
print
print 'number of words in list: ', no_lines_2
_______________ _______________ _______________ _______________ _______
thanks again for your help. that sorted out something that had really
been bugging me.
"sam" <py********@goo glemail.comwrit es:
hey everybody, this is my first time posting here. i'm pretty new to
python and programming in general (as you'll soon work out for
yourselves...)
On behalf of the entire Python community, *thank you* for putting this
disclaimer only in the body of your message and using the Subject
field for a descriptive summary of the topic.
There are far too many threads where the only information available
from the Subject is "I'm a newbie, help!" which is worse than useless
for knowing what the thread is about. Thank you for not following this
tedious trend!
--
\ "If sharing a thing in no way diminishes it, it is not rightly |
`\ owned if it is not shared." -- Saint Augustine |
_o__) |
Ben Finney
At Wednesday 20/9/2006 19:39, sam wrote:
>thanks again for your help. that sorted out something that had really been bugging me.
Now that your main problem is gone, just a few comments:
- python lists know their length, so you don't need explicit no_lines
and no_lines_2
- list_initial.re move(temp_str) is fairly slow - it has to *scan* the
list to locate temp_str. Just keep its index instead, and use del
list_initial[index]
- as a general sorting routine, that 'zzz....' does not look very
good, try to avoid it.
Gabriel Genellina
Softlab SRL
_______________ _______________ _______________ _____
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya! http://www.yahoo.com.ar/respuestas
gabriel,
Now that your main problem is gone, just a few comments:
- python lists know their length, so you don't need explicit no_lines
and no_lines_2
- list_initial.re move(temp_str) is fairly slow - it has to *scan* the
list to locate temp_str. Just keep its index instead, and use del
list_initial[index]
i will try rewriting it in that manner and comparing the times. i have
no feel so far for the relative speeds of different ways of doing
things, but i can see the importance of it.
- as a general sorting routine, that 'zzz....' does not look very
good, try to avoid it.
yes, it's hideous even to a novice. however, i need to go to bed now,
and if i hadn't got this to work before bed, i'd never get to sleep!
i'll have to mull it over and come up with something more elegant.
ben,
i used to work for a company where we exchanged a lot of e-mails, and
the titles were always 're: your e-mail', or 'project stuff', or 'file
for client' or suchlike. made it kind of difficult to find anything.
figured a similar principle might apply here... ; )
thanks again all,
sam
sam wrote:
I gues: no_lines=len(li st_initial)
for j in range(0, no_lines):
range returns 0, 1, 2, ..., no_lines-1
>
k = 0
while k < no_lines:
sorted_check = 0
if list_initial[k] < list_initial[k+1]:
When j gets its last value (no_lines-1) k has the same value and k+1
owerflows the list index range.
Try
for j in range(1, no_lines):
...
if list_initial[k-1] < list_initial[k]:
...
Tuomas
temp_str = list_initial[k]
elif list_initial[k] == list_initial[k+1]:
temp_str = list_initial[k]
elif list_initial[k] list_initial[k+1]:
temp_str = list_initial[k+1]
sorted_check = 1
k += 1
list_initial.re move(temp_str)
list_final.appe nd(temp_str)
no_lines -= 1
if sorted_check == 0:
break
.... This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Nicolas Girard |
last post by:
Hi,
Forgive me if the answer is trivial, but could you tell me how to achieve
the following:
{k1:,k2:v3,...} --> ,,,...]
The subtle point (at least to me) is to "flatten" values that are lists.
Thanks in advance,
|
by: Andrew MacIntyre |
last post by:
I'm seeing a bizarre situation where IndexErrors are being thrown with
"tuple index out of range" error strings.
The scenario is something like:
l =
for a, b in l:
...
|
by: Clark Choi |
last post by:
I ran the sample application called Petstore from msdn.
Everything went fine until I tested Update button on the
web form to update the database through Datagrid.
I have been searching the web to track down this error,
but none of them really explain this problem/solution
very well.
I tried hard to debug this, but debuggin itself was...
|
by: Dave |
last post by:
I'm getting the following error on an EditCommand event. This code is
running on production web farm and the thing is it doesn't happen all the
time. If I get this error, click the back button, and try again it works.
Sometimes it works the first time in.
I tried to step through the debugger running locally but I can't duplicate it.
...
|
by: Plissken.s |
last post by:
Hi
I have a problem which result in a "corrupted double-linked list
error", I would need some help in trouble shot this one:
Here is a stack track:
Thread (Suspended: Signal 'SIGABRT' received. Description:
Aborted.)
15 __kernel_vsyscall() 0xb7f25402
14 raise() 0x00646118
13 abort() 0x00647888
| |
by: dalaeth |
last post by:
I have searched Google high and low and haven't found anything that
works. Here's my problem, hopefully someone will be able to help!
I'm using 1.1 Framework, and ODP.NET 9.5.0.7 on a Windows 2003 Server.
I have a cached DataSet that I fill using an OracleDataAdapter. Changes
are made to that dataset in memory and as rows are changed,...
|
by: erikwickstrom |
last post by:
Hi all,
I'm sorry about the newbie question, but I've been searching all
afternoon and can't find the answer!
I'm trying to get this bit of code to work without triggering the
IndexError.
import shutil, os, sys
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
| |
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |