Sorted list - how to change it

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Lad

    #1

    Sorted list - how to change it

    I have a sorted list for example [1,2,3,4,5] and I would like to change
    it in a random way
    e.g [2,5,3,1,4] or [3,4,1,5,2] or in any other way except being
    ordered.
    What is the best/easiest
    way how to do it?

    Thank you for help
    L.

  • Wolfram Kraus

    #2
    Re: Sorted list - how to change it

    On 09.11.2006 10:52, Lad wrote:
    I have a sorted list for example [1,2,3,4,5] and I would like to change
    it in a random way
    e.g [2,5,3,1,4] or [3,4,1,5,2] or in any other way except being
    ordered.
    What is the best/easiest
    way how to do it?
    >
    Thank you for help
    L.
    >
    use random.shuffel:
    >>import random
    >>x = [1,2,3,4,5]
    >>random.shuffl e(x)
    >>x
    [1, 4, 2, 3, 5]
    >>random.shuffl e(x)
    >>x
    [4, 2, 1, 3, 5]

    see: http://docs.python.org/lib/module-random.html

    HTH,
    Wolfram

    Comment

    • Lad

      #3
      Re: Sorted list - how to change it


      Wolfram Kraus wrote:
      On 09.11.2006 10:52, Lad wrote:
      I have a sorted list for example [1,2,3,4,5] and I would like to change
      it in a random way
      e.g [2,5,3,1,4] or [3,4,1,5,2] or in any other way except being
      ordered.
      What is the best/easiest
      way how to do it?

      Thank you for help
      L.
      use random.shuffel:
      >
      >import random
      >x = [1,2,3,4,5]
      >random.shuffle (x)
      >x
      [1, 4, 2, 3, 5]
      >random.shuffle (x)
      >x
      [4, 2, 1, 3, 5]
      >
      see: http://docs.python.org/lib/module-random.html
      Wolfram,
      Thanks a lot for help
      L.

      Comment

      • Marc 'BlackJack' Rintsch

        #4
        Re: Sorted list - how to change it

        In <1163065969.593 970.165600@b28g 2000cwb.googleg roups.com>, Lad wrote:
        I have a sorted list for example [1,2,3,4,5] and I would like to change
        it in a random way
        e.g [2,5,3,1,4] or [3,4,1,5,2] or in any other way except being
        ordered.
        lst = [1, 2, 3, 4, 5]
        tmp = list(lst)
        while lst == tmp:
        random.shuffle( lst)

        If you *really* want to be sure it's not sorted after shuffling. :-)

        Ciao,
        Marc 'BlackJack' Rintsch

        Comment

        • Tim Chase

          #5
          Re: Sorted list - how to change it

          >I have a sorted list for example [1,2,3,4,5] and I would like to change
          >it in a random way
          >e.g [2,5,3,1,4] or [3,4,1,5,2] or in any other way except being
          >ordered.
          >What is the best/easiest
          > way how to do it?
          >
          use random.shuffel:
          >
          >>>import random
          >>>x = [1,2,3,4,5]
          >>>random.shuff le(x)
          >>>x
          [1, 4, 2, 3, 5]

          Just a caveat from past experience...wh ile the OP was talking
          about lists, for future reference random.shuffle( ) chokes on
          strings (and possibly tuples). It requires the ability to edit
          the target/parameter in place...a functionality that strings
          don't provide.

          Thus, for a "word jumble" program I was playing with, you can't
          just do

          word = 'hello'
          random.shuffle( word)

          but rather you have to list'ify the word, shuffle it, then pack
          it back together:

          word = 'hello'
          a = list(word)
          random.shuffle( a)
          word = ''.join(a)

          I remember seeing discussion on the list at one point of a
          MutableString class, which might be successfully passed to
          random.shuffle( ) without hiccuping.

          -tkc



          Comment

          • Marc 'BlackJack' Rintsch

            #6
            Re: Sorted list - how to change it

            In <pan.2006.11.09 .10.05.13.35244 3@gmx.net>, Marc 'BlackJack' Rintsch
            wrote:
            lst = [1, 2, 3, 4, 5]
            tmp = list(lst)
            while lst == tmp:
            random.shuffle( lst)
            Argh, that fails if the list is empty or contains just one item.

            lst = [1, 2, 3, 4, 5]
            if len(lst) 1:
            tmp = list(lst)
            while lst == tmp:
            random.shuffle( lst)

            Ciao,
            Marc 'BlackJack' Rintsch

            Comment

            • Christophe

              #7
              Re: Sorted list - how to change it

              Lad a écrit :
              I have a sorted list for example [1,2,3,4,5] and I would like to change
              it in a random way
              e.g [2,5,3,1,4] or [3,4,1,5,2] or in any other way except being
              ordered.
              What is the best/easiest
              way how to do it?
              >
              Thank you for help
              L.
              Not accepting that the shuffle outputs the same list as entered would
              make the random less random :)

              Comment

              • Nick Craig-Wood

                #8
                Re: Sorted list - how to change it

                Tim Chase <python.list@ti m.thechases.com wrote:
                Just a caveat from past experience...wh ile the OP was talking
                about lists, for future reference random.shuffle( ) chokes on
                strings (and possibly tuples). It requires the ability to edit
                the target/parameter in place...a functionality that strings
                don't provide.
                You can always use array.array('c' ), eg
                >>import random
                >>import array
                >>L = array.array('c' )
                >>L.fromstring( "hello")
                >>L
                array('c', 'hello')
                >>random.shuffl e(L)
                >>L
                array('c', 'elohl')
                >>L.tostring( )
                'elohl'

                Which is some way towards a mutable string...

                --
                Nick Craig-Wood <nick@craig-wood.com-- http://www.craig-wood.com/nick

                Comment

                Working...