Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 25th, 2005, 7:03 PM   #1
UnKnown X
Hobbyist Programmer
 
UnKnown X's Avatar
 
Join Date: Dec 2005
Location: Sandvika, Norway
Posts: 114
Rep Power: 0 UnKnown X is an unknown quantity at this point
Send a message via MSN to UnKnown X
Question about list error

When I try to do something like this:

y = ['1', '2', '3', '4', '5']    # example list
x = []                           # example list 2
i = 0
while i < len(y):
    x.append(y[i])
    i += 1
print x

I get an error message about trying to access an out-of-bounds element of y. This gets fixed if I do this:

y = ['1', '2', '3', '4', '5']    # example list
x = []                           # example list 2
i = 0
while i < len(y) - 1:            # fix is here
    x.append(y[i])
    i += 1
print x

But then, since i only goes to the second last item in y, the programme will only print:
['1', '2', '3', '4']

This is easily fixed by appending an extra item (such as 0) to the end of y, but I'm wondering why this happens.


Edit: wait a second. This list did work. I must've changed something when simplifying from the code from which I took it. Here's where it was originally:

def f(y):
    y.sort()
    y.append('0')
    i, j, k, flag = 0, 0, 0, 0
    x = []
    while j <= 9:
        try:
            while str(y[i]) == str(j) and i < len(y)-1:    # Here's the problem
                flag = 1
                i += 1
                k += 1
        except:
            print "Error!"
            return []
        if flag == 1:
            if k == 1:
                s = str(k) + ' time'
            else:
                s = str(k) + ' times'
            x.append([str(j),s])
        flag, k = 0, 0
        j += 1
    return x

Edit again: Oh, the problem must be in the str(y[i]) part of str(y[i]) == str(j), but still: why?
UnKnown X is offline   Reply With Quote
Old Dec 25th, 2005, 7:14 PM   #2
UnKnown X
Hobbyist Programmer
 
UnKnown X's Avatar
 
Join Date: Dec 2005
Location: Sandvika, Norway
Posts: 114
Rep Power: 0 UnKnown X is an unknown quantity at this point
Send a message via MSN to UnKnown X
Ah, problem found.

In str(y[i]) == str(j) and i < len(y)-1, it would check whether str(y[i]) == str(j) after i had been incremented, but before it could check if i < len(y), thus it would go beyond the list's boundary.

Putting the i < len(y) test before the str(y[i]) == str(j) test fixed it.
UnKnown X is offline   Reply With Quote
Old Dec 26th, 2005, 2:40 AM   #3
coldDeath
Expert Programmer
 
coldDeath's Avatar
 
Join Date: Aug 2005
Location: UK
Posts: 862
Rep Power: 3 coldDeath is on a distinguished road
Send a message via AIM to coldDeath Send a message via Yahoo to coldDeath
Nice job :-)
__________________
Join us at #programmingforums @ irc.freenode.net!

My software never has bugs. It just develops random features.
coldDeath is offline   Reply With Quote
Old Dec 26th, 2005, 3:59 AM   #4
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4 Arevos is on a distinguished road
You might want to think about using a dictionary to hold the counts of your items:
d = {}
for x in y:
    d[x] = d.get(x, 1) + 1
And perhaps giving your variables and functions longer and more descriptive names. It's all too easy to forget what does what several months down the line:
bag = {}
for item in list:
    bag[item] = bag.get(item, 1) + 1
(A bag being the technical name of the data struct your function creates.)
Arevos is offline   Reply With Quote
Old Dec 26th, 2005, 6:04 AM   #5
UnKnown X
Hobbyist Programmer
 
UnKnown X's Avatar
 
Join Date: Dec 2005
Location: Sandvika, Norway
Posts: 114
Rep Power: 0 UnKnown X is an unknown quantity at this point
Send a message via MSN to UnKnown X
Thanks for the replies!


I was simply doing this as a test. In slightly more serious projects, I do use more descriptive names.

I'll try the dictionaries, too. Thanks!
UnKnown X is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:34 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC