Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Python problem

Reply
Thread Tools

Python problem

 
 
WIWA
Guest
Posts: n/a
 
      08-18-2003

Hi all,



I'm trying to write the following in python:



j=0

for i in range(len(datumlijst)+1):

if (datumlijst[i+1]!=datumlijst):

datum[j]=datumlijst

j=j+1



print datum[:]



it complains about the part: datumlijst[i+1]!=datumlijst



it produces the following error:



File "hits_per_dag.py", line 24, in ?

datum[j]=datumlijst

IndexError: list assignment index out of range



Anyone know why this happens


--
Posted via http://dbforums.com
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      08-18-2003
WIWA wrote:
>
> I'm trying to write the following in python:


> j=0
>
> for i in range(len(datumlijst)+1):
> if (datumlijst[i+1]!=datumlijst):
> datum[j]=datumlijst
> j=j+1


> print datum[:]
>
> it complains about the part: datumlijst[i+1]!=datumlijst
>

You are comparing a list with a list entry which is probably unintended.

> datum[j]=datumlijst
>
> IndexError: list assignment index out of range
>

You seem to assume that a list grows automatically. That's not true in
Python.

Now here's my guess of your intentions:

datumlijst = [1,1,2,2,3,4,5,5,5,5,6,3]
datumlijst.sort()
datum = [datumlijst[0]]

for i in range(len(datumlijst)-1): # all list indexes start with 0
if datumlijst[i+1] != datumlijst[i]: # both indexed
datum.append(datumlijst[i+1]) # note the append()

print datum #no need for [:]

There may still sit a bug in the above. The following should be a little
more robust, e. g. no need to sort datumlijst, no extensive use of list
indexes:

from sets import Set
datumlijst = [1,1,2,2,3,4,5,5,5,5,6,3]
print list(Set(datumlijst))

This is Python 2.3 only, in 2.2 you can do it with a dictionary, using the
keys only.

Peter

PS: I see you've already fallen in love with whitespace
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: [Python-Dev] [python-committers] [RELEASED] Python 3.2 rc 1 R. David Murray Python 0 01-17-2011 02:23 PM
Re: [Python-Dev] [python-committers] [RELEASED] Python 3.2 rc 1 Senthil Kumaran Python 0 01-17-2011 10:31 AM
Re: [Python-Dev] [Python-3000] RELEASED Python 2.6a1 and 3.0a3 Martin v. L÷wis Python 0 03-01-2008 10:51 PM
Re: [Python-Dev] [Python-3000] RELEASED Python 2.6a1 and 3.0a3 Paul Moore Python 0 03-01-2008 10:39 PM
Searching comp.lang.python/python-list@python.org (was: UTF-8) skip@pobox.com Python 0 03-10-2007 02:50 PM



Advertisments