Skip to main content
 首页 » 编程设计

python之如何在 Python 中将一个列表拆分为两个唯一列表

2025年05月04日14bonelee

您好,我有一个列表如下: listt = ['a','b','c','d','e','f','g','h','i','j','k','l' ,'','n','o'] 15名成员。

我想把它变成 3 个列表,我使用了这段有效的代码,但我想要唯一的列表。这给了我 3 个具有共同成员的列表。

import random 
 
listt = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o'] 
print(random.sample(listt,5)) 
print(random.sample(listt,5)) 
print(random.sample(listt,5)) 

请您参考如下方法:

试试这个:

from random import shuffle 
 
 
def randomise(): 
    listt = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o'] 
    shuffle(listt) 
    return listt[:5], listt[5:10], listt[10:] 
 
print(randomise()) 

这将打印(例如,因为它是随机的):

(['i', 'k', 'c', 'b', 'a'], ['d', 'j', 'h', 'n', 'f'], ['e', 'l', 'o', 'g', 'm'])