By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,235 Members | 1,022 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,235 IT Pros & Developers. It's quick & easy.

Creating a Null list, seems to create a null tuple instead

P: 3
Please don't critique my algorithm as I'm a rank newbie (or go ahead, I can take it :) ). But I can't figure the following out.

Expand|Select|Wrap|Line Numbers
  1. def shuffle():
  2.     """ Return a list of indices from 0-51 with no repeats."""
  3.     index = range (0,52)
  4.     shuffled_list = list()   # or shuffled_list = [] does the same
  5.     while index:
  6.         card = random.choice(index)
  7.         index.remove(card)
  8.         shuffled_list.append(card)
  9. #        print shuffled_list
  10.     return shuffled_list
  11.  
shuffled_list always seems to be defined as () when I look in the debugger, so I continually get

File "C:\Python25\cards.py", line 29, in shuffle
while index:
AttributeError: 'tuple' object has no attribute 'append'

I have no idea whats wrong here.
May 4 '07 #1
Share this Question
Share on Google+
5 Replies


P: 21
Hi
I am also new to python.
I tried your code, it works fine for me.
(I use Python 2.5).

Regards,
Prasannaa
May 4 '07 #2

P: 3
Hi
I am also new to python.
I tried your code, it works fine for me.
(I use Python 2.5).

Regards,
Prasannaa
Well, interestingly, I exited Winpython, restarted it and it works for me too now. I fixed one problem in preceeding code at the same time. My C background had intruded and I had
Expand|Select|Wrap|Line Numbers
  1. deck = [];
  2.  
Does anyone know what that causes? I got no errors but perhaps it messed something up in the intepreter and it couldn't recover?

Thanks!
May 4 '07 #3

bvdet
Expert Mod 2.5K+
P: 2,851
Well, interestingly, I exited Winpython, restarted it and it works for me too now. I fixed one problem in preceeding code at the same time. My C background had intruded and I had
Expand|Select|Wrap|Line Numbers
  1. deck = [];
  2.  
Does anyone know what that causes? I got no errors but perhaps it messed something up in the intepreter and it couldn't recover?

Thanks!
Since 'list()' returns '[]', I cannot explain why you had the tuple problem. Your algorithm looks fine. For the record, the random module has other functions that do basically the same thing:
Expand|Select|Wrap|Line Numbers
  1. >>> deck = range(0,52)
  2. >>> random.shuffle(deck)
  3. >>> deck
  4. [41, 10, 23, 46, 14, 25, 5, 37, 9, 11, 3, 47, 1, 49, 22, 20, 29, 15, 51, 8, 4, 18, 17, 26, 36, 43, 34, 0, 48, 2, 50, 6, 16, 33, 28, 24, 38, 44, 35, 27, 42, 31, 7, 45, 40, 12, 13, 21, 19, 39, 32, 30]
  5. >>> 
OR
Expand|Select|Wrap|Line Numbers
  1. >>> random.sample(range(0,52), 52)
  2. [10, 26, 7, 45, 46, 48, 16, 1, 34, 32, 6, 35, 30, 18, 0, 21, 49, 8, 40, 29, 4, 36, 11, 15, 42, 28, 5, 3, 25, 27, 20, 9, 43, 17, 24, 39, 22, 33, 38, 2, 47, 51, 50, 13, 12, 44, 19, 31, 37, 14, 41, 23]
  3. >>> 
May 4 '07 #4

P: 3
Since 'list()' returns '[]', I cannot explain why you had the tuple problem. Your algorithm looks fine. For the record, the random module has other functions that do basically the same thing:
Expand|Select|Wrap|Line Numbers
  1. >>> deck = range(0,52)
  2. >>> random.shuffle(deck)
  3. >>> deck
  4. [41, 10, 23, 46, 14, 25, 5, 37, 9, 11, 3, 47, 1, 49, 22, 20, 29, 15, 51, 8, 4, 18, 17, 26, 36, 43, 34, 0, 48, 2, 50, 6, 16, 33, 28, 24, 38, 44, 35, 27, 42, 31, 7, 45, 40, 12, 13, 21, 19, 39, 32, 30]
  5. >>> 
OR
Expand|Select|Wrap|Line Numbers
  1. >>> random.sample(range(0,52), 52)
  2. [10, 26, 7, 45, 46, 48, 16, 1, 34, 32, 6, 35, 30, 18, 0, 21, 49, 8, 40, 29, 4, 36, 11, 15, 42, 28, 5, 3, 25, 27, 20, 9, 43, 17, 24, 39, 22, 33, 38, 2, 47, 51, 50, 13, 12, 44, 19, 31, 37, 14, 41, 23]
  3. >>> 
EXCELLENT, thanks!
May 7 '07 #5

bartonc
Expert 5K+
P: 6,596
Well, interestingly, I exited Winpython, restarted it and it works for me too now.
I've heard of this type of old vs new namespace problem in PythonWin (never experienced it since I only used that IDE for a day or two). There are issues with import vs reload (I never use those either) IDLE's Run Module has always worked for me. Perhaps you weren't using PythonWin's Run command???
I fixed one problem in preceeding code at the same time. My C background had intruded and I had
Expand|Select|Wrap|Line Numbers
  1. deck = [];
  2.  
Does anyone know what that causes? I got no errors but perhaps it messed something up in the intepreter and it couldn't recover?

Thanks!
Python will create an empty list. But then, if you
Expand|Select|Wrap|Line Numbers
  1. deck = 'abc'
(or something) Python simply re-assigns the name to the new object and if that was the only reference to the empty list, it is marked for Garbage Collection.
May 7 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.