简体   繁体   中英

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM