简体   繁体   English

如何在Python中将列表分为两个唯一列表?

[英]How can I split a list in two unique lists in Python?

Hi I have a list as following: listt = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o'] 15 members. 嗨,我有一个列表如下:listt = ['a','b','c','d','e','f','g','h','i','j' ,'k','l','m','n','o'] 15位成员。

I want to turn it into 3 lists, I used this code it worked but I want unique lists. 我想将其转换为3个列表,我使用了有效的代码,但我想要唯一的列表。 this give me 3 lists that have mutual members. 这给了我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))

Try this: 尝试这个:

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())

This will print (for example, since it is random): 这将打印(例如,由于它是随机的):

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

Use [:] for slicing all members out of the list which basically copies everything into a new object. 使用[:]从列表中切出所有成员,基本上将所有内容复制到一个新对象中。 Alternatively just use list(<list>) which copies too: 另外,也可以使用list(<list>)进行复制:

print(random.sample(listt[:],5))

In case you want to shuffle only once, store the shuffle result into a variable and copy later: 如果您只想随机播放一次,请将随机播放结果存储到变量中,然后再复制:

output = random.sample(listt,5)
first = output[:]
second = output[:]
print(first is second, first is output)  # False, False

and then the original list can be modified without the first or second being modified. 然后就可以修改原始列表,而无需修改第firstsecond列表。

For nested lists you might want to use copy.deepcopy() . 对于嵌套列表,您可能需要使用copy.deepcopy()

Instead of sampling your list three times, which will always give you three independent results where individual members may be selected for more than a single list, you could just shuffle the list once and then split it in three parts. 无需对列表进行三次抽样(这总是会为您提供三个独立的结果,其中可以为单个列表选择多个成员),您可以将列表随机播放一次 ,然后将其分为三个部分。 That way, you get three random subsets that will not share any items: 这样,您将获得三个不会共享任何项目的随机子集:

>>> random.shuffle(listt)
>>> list[0:5]
>>> listt[0:5]
['b', 'a', 'f', 'e', 'h']
>>> listt[5:10]
['c', 'm', 'g', 'j', 'o']
>>> listt[10:15]
['d', 'l', 'i', 'n', 'k']

Note that random.shuffle will shuffle the list in place, so the original list is modified. 请注意, random.shuffle将对列表进行随机排序,因此原始列表将被修改。 If you don't want to modify the original list, you should make a copy first. 如果您不想修改原始列表,则应先进行复制。

If your list is larger than the desired result set, then of course you can also sample your list once with the combined result size and then split the result accordingly: 如果列表大于期望的结果集,那么当然您也可以使用合并后的结果大小对列表进行一次采样,然后相应地拆分结果:

>>> sample = random.sample(listt, 5 * 3)
>>> sample[0:5]
['h', 'm', 'i', 'k', 'd']
>>> sample[5:10]
['a', 'b', 'o', 'j', 'n']
>>> sample[10:15]
['c', 'l', 'f', 'e', 'g']

This solution will also avoid modifying the original list, so you will not need a copy if you want to keep it as it is. 该解决方案还将避免修改原始列表,因此,如果您希望保持原样,则不需要副本。

If it doesn't matter to you which items go in each list, then you're better off partitioning the list into thirds: 如果对每个列表中的项无关紧要,那么最好将列表分为三部分:

In [23]: L =  ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o']                                                                                                     

In [24]: size = len(L)                                                                                                                                                          

In [25]: L[:size//3]                                                                                                                                                            
Out[25]: ['a', 'b', 'c', 'd', 'e']

In [26]: L[size//3:2*size//3]                                                                                                                                                   
Out[26]: ['f', 'g', 'h', 'i', 'j']

In [27]: L[2*size//3:]                                                                                                                                                          
Out[27]: ['k', 'l', 'm', 'n', 'o']

If you want them to have random elements from the original list, you'll just need to shuffle the input first: 如果希望它们具有原始列表中的随机元素,则只需要先将输入随机排序即可:

random.shuffle(L)

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

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