简体   繁体   English

从列表列表中随机选择X个列表

[英]Randomly select X number of lists from a list of lists

I have something like the following: 我有以下内容:

pen = [1, 2, 3, 4]
pencil = [2,3,4,5]
paper = [3,4,5,6]
group_of_items = [pen, pencil, paper]

I want to randomly select a certain number of lists from this list of lists so that the result is something like this: 我想从这个列表列表中随机选择一定数量的列表,结果是这样的:

[pencil, pen]

I found the following from another question (altered to match my situation). 我从另一个问题中找到了以下内容(根据我的情况进行了修改)。

import random

pen = [1, 2, 3, 4]
pencil = [2,3,4,5]
paper = [3,4,5,6]

group_of_items = [pen, pencil, paper]

num_to_select = 2
list_of_random_items = random.sample(group_of_items, num_to_select)
print(list_of_random_items)

It gives something like this. 它给出了这样的东西。

[[2, 3, 4, 5], [1, 2, 3, 4]]

So, it's close but no cigar. 所以,它很接近,但没有雪茄。 I also found this. 我也找到了这个。

import numpy as np

pen = [1, 2, 3, 4]
pencil = [2,3,4,5]
paper = [3,4,5,6]
group_of_items = [pen, pencil, paper]
num_to_select = 2

random_list = np.random.choice(group_of_items, num_to_select, replace=False)

print(random_list)

But it doesn't work with a list of lists (multi-dimensional). 但它不适用于列表列表(多维)。

How can I accomplish my goal? 我怎样才能实现目标?

Oh, and I don't want any repeats. 哦,我不想重复。

Note: my coding experience is rather limited. 注意:我的编码经验相当有限。 I mostly copy and paste what I find online, only making small changes. 我主要复制并粘贴我在网上找到的内容,只进行了一些小改动。

Edit : The above is simply a quickly thrown together test. 编辑 :以上只是一个快速抛出的测试。 What I've built is a Twitter tweet bot using PythonAnywhere. 我构建的是一个使用PythonAnywhere的Twitter推文机器人。 It works wonderfully as is, but I want to add a more random functionality to it. 它的工作原理非常好,但我想为它添加一个更随机的功能。

I have lists of tweets in a Google spreadsheet that I pull over to a Python list, like so: 我在Google电子表格中列出了推文列表,我将其移至Python列表中,如下所示:

quotes = tweet_sheet.col_values(3)

I have several lists like this that I put together in one master list of lists. 我有几个这样的列表,我把它放在一个主列表中。 But I don't want to tweet from every list every time I run the program. 但是每次运行程序时我都不想从每个列表中发推文。

Right now I use something like this. 现在我使用这样的东西。

sources = [tips,feed,quotes... etc...

I want to pick x number of lists from the master list of lists to use when the program runs. 我想从程序运行时使用的主列表列表中选择x个列表。 (That's worded kind of funny) (那措辞有点搞笑)

I'm guessing from the comments so far that what I have above would work. 到目前为止,我从评论中猜测,我上面的内容会起作用。 With a little more tweaking of the rest of the code, that is. 稍微调整其余代码,就是这样。

Here is an approach using dictionaries: 这是一种使用字典的方法:

>>> pen = [1, 2, 3, 4]
>>> pencil = [2,3,4,5]
>>> paper = [3,4,5,6]
>>> item_dict = {'pen':pen, 'pencil':pencil, 'paper':paper}
>>> import random
>>> item_names = list(item_dict.keys())
>>> item_names
['pencil', 'pen', 'paper']
>>> sample = random.sample(item_names,2)
>>> sample
['pencil', 'pen']
>>> item_dict[sample[0]]
[2, 3, 4, 5]
>>> item_dict[sample[1]]
[1, 2, 3, 4]

So now you have an association between strings and lists : 所以现在你在字符串列表之间有一个关联:

>>> "The first list sampled was {}. Here's the list {}".format(sample[0], item_dict[sample[0]])
"The first list sampled was pencil. Here's the list [2, 3, 4, 5]"
>>> 

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

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