Skip to content
Advertisement

randomizing list and exporting the results as unique set of 4 strings

I have wanted to randomize list of strings, 9 different strings and then printing out the strings into unique set of four then exporting it into txt file. Here is what I have tried so far, but I am not sure how to set them into groups of four.

import random
a = "0xa5"
b = "0x1"
c = "0xE5924"
d = "0xE59"
e = "0xC60aE"
g = "0xd7F1DF"
h = "0xDEF1717"
i = "0xA102072A4C07F9"
j = "0x934A0E2c6C427D9F25"



my_list = [a,b,c,d,e,g,h,i,j]
random.shuffle(my_list)
myset = set(my_list)
print(myset)

I want the print outcome to come out in an example of:

["0xa5","0x1","0xE5924","0xE59"],
["0xa5","0x1","0xE5924","0xA102072A4C07F9"],

Advertisement

Answer

I did it with a while loop, just picking random numbers of the set you get.

oldarray= my_list
newarray=[]
count=0
while count<4:
    num=random.randint(0,8)
    newarray.append(oldarray[num])
    count+=1
print(newarray)
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement