Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: the del function

1 view
Skip to first unread message

Matt McCredie

unread,
Aug 27, 2007, 12:52:50 PM8/27/07
to Lamonte Harris, pytho...@python.org
> 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

0 new messages