convert a list to a string

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

    convert a list to a string

    number = random.sample(r ange(count), 1)

    This makes 'number' into a one entry list (I don't know why as 'count'
    is an integer). Is there an easy way to convert 'number' back to an int?

    TIA

  • Paul Rubin

    #2
    Re: convert a list to a string

    Bart Nessux <bart_nessux@ho tmail.com> writes:[color=blue]
    > number = random.sample(r ange(count), 1)
    >
    > This makes 'number' into a one entry list (I don't know why as 'count'
    > is an integer). Is there an easy way to convert 'number' back to an
    > int?[/color]

    number = number[0]

    But why are you using random.sample for that? And making a list of
    size count, just to throw it away? It sounds like you just want a
    random number between zero and count. Do that with:

    number = random.randint( count)

    Comment

    • Bart Nessux

      #3
      Re: convert a list to a string

      Paul Rubin wrote:[color=blue]
      > Bart Nessux <bart_nessux@ho tmail.com> writes:
      >[color=green]
      >>number = random.sample(r ange(count), 1)
      >>
      >>This makes 'number' into a one entry list (I don't know why as 'count'
      >>is an integer). Is there an easy way to convert 'number' back to an
      >>int?[/color]
      >
      >
      > number = number[0]
      >
      > But why are you using random.sample for that? And making a list of
      > size count, just to throw it away? It sounds like you just want a
      > random number between zero and count. Do that with:
      >
      > number = random.randint( count)[/color]

      How does randint and sample differ? I was under the impression that
      sample was more random.



      Comment

      • Bart Nessux

        #4
        Re: convert a list to a string

        Paul Rubin wrote:[color=blue]
        > Bart Nessux <bart_nessux@ho tmail.com> writes:
        >[color=green]
        >>number = random.sample(r ange(count), 1)
        >>
        >>This makes 'number' into a one entry list (I don't know why as 'count'
        >>is an integer). Is there an easy way to convert 'number' back to an
        >>int?[/color]
        >
        >
        > number = number[0]
        >
        > But why are you using random.sample for that? And making a list of
        > size count, just to throw it away? It sounds like you just want a
        > random number between zero and count. Do that with:
        >
        > number = random.randint( count)[/color]

        Forgot to say thanks! the number[0] thing is great!

        Comment

        • Paul Rubin

          #5
          Re: convert a list to a string

          Bart Nessux <bart_nessux@ho tmail.com> writes:[color=blue]
          > How does randint and sample differ? I was under the impression that
          > sample was more random.[/color]

          No, they both use the same underlying generator.

          randint gives you one random number, which is what you want.
          sample takes a list and selects some elements from it.

          As I mentioned in another post, the underlying generator is designed
          to have good statistical properties for doing things like simulations.
          It's not designed to withstand someone actively trying to predict the
          next random number based on the previous output stream. If you're using
          it in an application like a high-stakes online game, where players have
          significant incentive to predict the output, then you should get your
          random numbers a different way. Doing that kind of thing properly
          takes a lot of skill though--it's not something newbies should attempt.

          Comment

          • Jeff Epler

            #6
            Re: convert a list to a string

            On Thu, Jan 08, 2004 at 10:31:16PM -0500, Bart Nessux wrote:[color=blue]
            > How does randint and sample differ? I was under the impression that
            > sample was more random.[/color]

            sample is useful when you want to choose several items from the
            population. The degenerate case
            random.sample(r ange(100), 1)
            is equivalent to
            [random.choice(r ange(100))]
            is equivalent to
            [random.randrang e(100)]

            here's where sample is useful: Pick a TLA, but don't repeat any letters:[color=blue][color=green][color=darkred]
            >>> "".join(random. sample(string.u ppercase, 3))[/color][/color][/color]
            'CLA'
            You can't sample more than your whole population:[color=blue][color=green][color=darkred]
            >>> "".join(random. sample(string.u ppercase, 28))[/color][/color][/color]
            ValueError: sample larger than population
            And here's another way to take a random permutation of the alphabet:[color=blue][color=green][color=darkred]
            >>> "".join(random. sample(string.u ppercase, 26))[/color][/color][/color]
            'STFVCQHDLOUNER PYIMABGJXKZW'
            No letters are repeated, unlike with random.choice:[color=blue][color=green][color=darkred]
            >>> "".join([random.choice(s tring.uppercase ) for i in
            >>> range(26)])[/color][/color][/color]
            'ZMXYCNLDCQAIYT AVPPTSGABNVU'
            (the odds of *not* having a duplicate letter here are quite small,
            though I don't know offhand just how small--maybe 26!/(26**25)?)

            Jeff

            Comment

            • Francis Avila

              #7
              Re: convert a list to a string

              Bart Nessux wrote in message <3FFE21A4.70402 08@hotmail.com> ...[color=blue]
              >Forgot to say thanks! the number[0] thing is great![/color]

              Here's something a bit more mysterious, but sometimes convenient, e.g. with
              struct (which always returns a tuple):
              [color=blue][color=green][color=darkred]
              >>> a, = [1] #note trailing comma
              >>> a[/color][/color][/color]
              1

              --
              Francis Avila

              Comment

              Working...