I'm trying to generate a random string using the elements of a different string, with the same length.
Ex) String: AGAACGC
I want a random string using only elements from the string above
Right now I have:
import random
seq = 'AGAACGC'
length = len(seq)
for blah in range(len(seq)): #Apologies for the lame variable names
x = random.randint(0,length - 1)
print seq[x]
And of course this will give me something like:
A
A
G
A
C
A
A
How would I alter the program so that it prints out my random string on one line?
I'm hoping for a relatively simple answer, as I'm still a beginner in python, so I don't want to get too fancy. But any help at all would be appreciated.
And is there a way to repeat the randomization 'n' number of times?
Say instead of 1 random string, I wanted 4 random strings on different lines. Is there an easy way doing this? (My newbish mind is telling me to repeat the loop 'n' times...but it needs to be a changeable number of randomized strings)
Sorry for asking. I'd really appreciate some help. I'm stuck on a bigger problem because I don't know how to do these steps.
That's exactly what
random.sample
is meant for:In Python 3, there is a simple way to print a character without a newline, but in Python 2, there isn't.* So, the simplest way to do this is to build up the string, then print it at the end:
* There are two less simple ways: You can use
from __future__ import print_function
and then use Python 3print
syntax instead of Python 2, or you can usesys.stdout.write(seq[x])
for each character and thensys.stdout.write('\n')
andsys.stdout.flush()
at the end. But I don't think you want either of those.However, there are a number of ways to improve this:
randrange(length)
instead ofrandint(0, length - 1)
.random.choice(seq)
instead of usingrandrange
orrandint
in the first place._
instead ofblah
for "don't care" variables.*for _ in seq:
, notfor _ in range(len(seq)):
.''.join()
at the end, instead of building up a string.Putting that all together:
* Unless you're using
gettext
for i18n, or something else that gives_
a special meaning. Most projects that do that come up with their own convention for "don't care" names—maybe__
, ordummy
. And of courseblah
would be fine for such a project, as long as you used it consistently.As for repeating it, your newbish mind is right; just use a loop. For example (taking the second version, but it'll work just as well with the first):
That'll print out 4 different random strings. And if you set
n
to 100, it'll print 100 different random strings.You may want to write this in a function:
And then you can call it like:
But either way, your basic intuition is right on track.