简体   繁体   中英

shorter way to merge, shuffle and take first n elements of a list

I'm doing that (see code) and i'm thinking that there will be a shorter way

    fd_list = list(set(fd_list1 + fd_list2 + fd_list3))
    shuffle(fd_list)
    fd_list = fd_list[:10]

what i want to do is: concatenate three list's, shuffle them and take the first 10 elements. Alternativ i could merge the three list's and take randomly 10 elements?!Maybe?! I don't know how. By the way the result list have to be unique.

EDIT*: example list1 = [ 1, 2, 3, 6], list2 = [], list3 = [4, 5, 6, 6, 7, 8, ,8]

a result should maybe look like this...

result = [3, 4, 5, 1 ,8 ,2 ,7 ,6] 

i think the "sample" solution would be the best. Thanks a lot!

random.sample(set(fd_list1 + fd_list2 + fd_list3), 10)

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