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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JimDibb
    New Member
    • May 2007
    • 3

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

    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.

    Code:
    def shuffle():
        """ Return a list of indices from 0-51 with no repeats."""
        index = range (0,52)
        shuffled_list = list()   # or shuffled_list = [] does the same
        while index:
            card = random.choice(index)
            index.remove(card)
            shuffled_list.append(card)
    #        print shuffled_list
        return shuffled_list
    shuffled_list always seems to be defined as () when I look in the debugger, so I continually get

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

    I have no idea whats wrong here.
  • Prasannaa
    New Member
    • May 2007
    • 21

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

    Regards,
    Prasannaa

    Comment

    • JimDibb
      New Member
      • May 2007
      • 3

      #3
      Originally posted by Prasannaa
      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
      Code:
      deck = [];
      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!

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Originally posted by JimDibb
        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
        Code:
        deck = [];
        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:
        Code:
        >>> deck = range(0,52)
        >>> random.shuffle(deck)
        >>> deck
        [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]
        >>>
        OR
        Code:
        >>> random.sample(range(0,52), 52)
        [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]
        >>>

        Comment

        • JimDibb
          New Member
          • May 2007
          • 3

          #5
          Originally posted by bvdet
          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:
          Code:
          >>> deck = range(0,52)
          >>> random.shuffle(deck)
          >>> deck
          [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]
          >>>
          OR
          Code:
          >>> random.sample(range(0,52), 52)
          [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]
          >>>
          EXCELLENT, thanks!

          Comment

          • bartonc
            Recognized Expert Expert
            • Sep 2006
            • 6478

            #6
            Originally posted by JimDibb
            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
            Code:
            deck = [];
            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
            Code:
            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.

            Comment

            Working...