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

Time:09-12

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"],

CodePudding user response:

import random
a = "0xa5"
b = "0x1"
c = "0xE5924"
d = "0xE59"
e = "0xC60aE"
g = "0xd7F1DF"
h = "0xDEF1717"
i = "0xA102072A4C07F9"
j = "0x934A0E2c6C427D9F25"
print(list(set(random.sample([a,b,c,d,e,g,h,i,j],4))))

CodePudding user response:

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)
  • Related