the del function

Matt McCredie mccredie at gmail.com
Mon Aug 27 12:52:50 EDT 2007


> For some odd reason the del array[ray] isn't actually deleting the array
> item in the list I get the following output:
>
> C:\Documents and
> Settings\program\Desktop\python\pygame>remix.py
> [2, 2, 2, 4, 4, 4, 4, 3, 3, 3, 3]
> [2, 2, 2, 4, 4, 4, 4, 3, 3, 3, 3]
> [2, 2, 2, 2, 4, 4, 4, 3, 3, 3, 3]
> [2, 2, 2, 2, 4, 4, 4, 3, 3, 3, 3]
> [2, 2, 4, 4]
>
> The _red function is fine, but the del function isn't working.  What did I
> do wrong?

The code is doing what you told it to:

[code]
while x < 4:
    array = single_players[4:17] # <-- you are re-creating `array' in every loop
    length = len(array) - 1
    ray = random.randint(0,length)
    _red[x] = array[ray]
    del array[ray]
    print array
    x = x + 1
print _red
[/code]

My guess is that you want this:

[code]
array = single_players[4:17]
while x < 4:
    length = len(array) - 1
    ray = random.randint(0,length)
    _red[x] = array[ray]
    del array[ray]
    print array
    x = x + 1
print _red
[/code]

I'm sure you could do something with random.shuffle or random.choice
though that would be much cleaner. It isn't entirely clear what you
are trying to do though, so I'm stopping short of posting any code.

Matt



More information about the Python-list mailing list