i am a little confused on how to generate with the ramdom function in python a list of length 10000 that randomly repeats the same 2 variables. Suppose once the randomly generated list is [a,b,a,a,a,a,b,b....] and the next time it renders [b,a,b,b,a,a,b,b...]. Can anyone help me on this one?
CodePudding user response:
You can create a function the receives the desired length of the list and 2 characters, and randomly fills the list with the 2 characters.
for example:
import random
def generate_random_list(length: int, char1: str, char2: str) -> list[str]:
return [char1 if random.randint(1, 2) == 1 else char2 for _ in range(length)]
print(generate_random_list(10000, "a", "b"))
CodePudding user response:
Hi i_am_trying and welcome to python programming.
One of the fun things about python is that it has a rich library of really handy functions. Samwise gave us a great example of this:
random.choices(['a', 'b'], k=10000)
That's gets the job done very nicely (and I learned a new function, thanks Samwise). But that's not going to teach you programming.
Omer gives you a little more insight in how to get the job done - plus Omer gave you a function you can re-use (although, random.choices() appears to be more general).
Omer's response has a lot of the fun python syntax (list comprehensions, a function with type hints, and the tertiary operator), but it's easy to miss the logic in there when you are new to the language.
It's best, in this case, to start with a basic expression. You want to do something 10,000 times, so it makes sense to know about a for
loop. Here's a for
that does something 10,000 times:
for count in range(10000):
# do something
pass
Above, I just put the dummy expression pass
in there so the for
loop would be syntactically correct.
Anyway, what do you want to do 10,000 times? You want to add a random letter to a list. So, here's the logic I recommend:
Initialize your results list
Run you for loop
In the for loop, add a random value to your list
That's pretty awful pseudo code, but here's some code that implements it:
results = list() # you could also write results = []
for count in range(10000):
if random.randint(0,1) == 0:
results.append('a')
else:
results.append('b')
print(results)
Or you can just use the randchoice() function with basically makes the if
unnecessary:
results = []
for count in range(10000):
results.append(random.choice(['a','b']))
Or you can use another canned method to do away with all of the code altogther (and we're back to Samwise's awesome answer:
results = random.choices(['a','b'], k=10000)
CodePudding user response:
Here's an interesting approach at this problem using the random.shuffle()
function. shuffle()
re-arranges the items in a list randomly. With this function, the problem is to create a list with 5,000 'a' values and 5,000 'b' values and shuffle it. Here's some code (for much fewer values):
>>> random.shuffle( results := [ 'a' , 'b' ] * 10 )
>>> results
['b', 'a', 'b', 'b', 'a', 'a', 'a', 'a', 'b', 'b', 'a', 'a', 'b', 'b', 'b', 'a', 'b', 'a', 'b', 'a']
The first line creates a list with 10 a's and 10 b's. The list is assigned to the results variable name (alias). I needed to give the list a name so I could display it later so I used the walrus :=
operator.
I still think SamWise has the best approach using random.choices()
but I wanted to provide a slightly different approach.