Issue
Say that I have a list with elements:
listA = [1, 2, 3, 4, 5]
print(listA) returns:
[1, 2, 3, 4, 5]
I want to randomize the elements in listA
, but then store this information in listB
import random
listB = random.shuffle(listA)
but if I print listB
, it returns None
print(listB)
None
Instead, if I print listA
, it returns the randomized list
print(listA)
[3, 2, 1, 4, 5]
What is going on here? How to I re-declare a variable with a new name after applying a function?
Solution
You can use random.sample function.
import random
listA = [1, 2, 3, 4, 5]
listB = random.sample(listA, len(listA))
Answered By - zerg468
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.