473,395 Members | 1,629 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,395 software developers and data experts.

Python problem


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
Jul 18 '05 #1
1 4496
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 :-)
Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: David MacQuigg | last post by:
I am starting a new thread so we can avoid some of the non-productive argument following my earlier post "What is good about Prothon". At Mr. Hahn's request, I will avoid using the name "Prothon"...
68
by: Lad | last post by:
Is anyone capable of providing Python advantages over PHP if there are any? Cheers, L.
112
by: mystilleef | last post by:
Hello, What is the Pythonic way of implementing getters and setters. I've heard people say the use of accessors is not Pythonic. But why? And what is the alternative? I refrain from using them...
0
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 417 open ( -6) / 3565 closed (+12) / 3982 total ( +6) Bugs : 960 open ( -3) / 6498 closed (+19) / 7458 total (+16) RFE : 266 open...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
Oralloy
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,...
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...

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.