简体   繁体   English

合并,洗牌和获取列表的前n个元素的更短方法

[英]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. 我想做的是:连接三个列表,将它们洗牌并取前10个元素。 Alternativ i could merge the three list's and take randomly 10 elements?!Maybe?! Alternativ我可以合并三个列表,并随机取10个元素? 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] 编辑*:示例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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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