【问题标题】:Randomly select x number of elements from a list从列表中随机选择 x 个元素
【发布时间】:2014-02-07 04:03:46
【问题描述】:

我正在尝试编写一个代码,该代码将在列表的 1 到 4 个元素之间随机选择 50 次。我正在使用的列表是['nu', 'ne', 'na', 'ku', 'ke', 'ka']

所以本质上,我希望它输出类似

nukuna
ke
keka
nuka
nane
nanenu
nu
nukekanu
kunu
...

50 次

【问题讨论】:

  • 您使用什么脚本/编程语言?
  • 你使用什么语言?
  • 重复:kukukuku 会被允许吗?
  • 允许重复

标签: python list random selection


【解决方案1】:
int i=0;
StringBuffer stb=new StringBuffer();
String[] arr= {"nu", "ne", "na", "ku", "ke", "ka"};
while(i<50){
int idx = new Random().nextInt(arr.length);
 stb.append(arr[idx]);

i++;
}

【讨论】:

  • 这不符合要求。它只是分发 50 个随机元素,而不是最多包含 4 个随机元素的 50 个字符串。
  • 哦,这是java代码> 因为你后面提到了语言。
【解决方案2】:

在 Python 中:

import random

input  = [...] # Your input strings
output = ''

random.seed() # Seed the random generator

for i in range(0,len(input)):
    N = 1+random.randrange(4) # Choose a random number between 1 and 4
    for j in range(0,N): # Choose N random items out of the input
        index = random.randrange(len(input)-j)
        temp = input[index]
        input[index] = input[len(input)-j-1]
        input[len(input)-j-1] = temp
        output += temp
    output += ' '

print output

在 C 中:

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <time.h>

char* input[NUM_OF_INPUT_STRINGS] = {...}; // Your input strings
char output[MAX_SIZE_OF_OUTPUT+1];

// Seed the random generator
srand((unsigned int)time(NULL));

for (int i=0; i<NUM_OF_INPUT_STRINGS; i++)
{
    // Set the output string empty
    output[0] = 0;
    // Choose a random number between 1 and 4
    int N = 1+(rand()%4);
    // Choose N random items out of the input
    for (int j=0; j<N; j++)
    {
        int index = rand()%(NUM_OF_INPUT_STRINGS-j);
        char* temp = input[index];
        input[index] = input[NUM_OF_INPUT_STRINGS-j-1];
        input[NUM_OF_INPUT_STRINGS-j-1] = temp;
        strcat(output,temp);
    }
    // Print the output
    printf("%s ",output);
}

【讨论】:

  • 谢谢,但我在 Python 中收到“列表索引超出范围”的错误。我该如何解决?
  • 对我来说很好;您是否验证了input 数组包含NUM_OF_INPUT_STRINGS 字符串?
  • 更好 - 我将NUM_OF_INPUT_STRINGS 替换为len(input),因此您可以在input 数组中放入任意数量的字符串...
  • 在 Python 代码中应用了一个小修复,以避免多次重复相同的字符...
  • for i in range(0,len(input)): 应替换为for i in range(0,len(input)-1):
【解决方案3】:

试试这个 Python 代码:

import random

my_list = ['nu', 'ne', 'na', 'ku', 'ke', 'ka']

for i in xrange(0,50):
    tmp_string = ''
    count = random.randrange(1,4)      # choose a random between 1 and 4
    for j in xrange(0, count):
         # add a random member of the list to the temporary string
         tmp_string = tmp_string + random.choice(my_list)
    print tmp_string         # print each final string

【讨论】:

  • 我看到他编辑了您的帖子并对其投了反对票,就像他对我的帖子所做的那样。该用户应被禁止访问本网站。我投票是为了“补偿”你的损失。
  • 谢谢。顺便说一句,除了您在对他的回答的评论中提到的内容(事实上,他在发布一年多后在这里发布了与以前的答案相同的答案),您可能还想检查(并且可能恢复)他对您自己的答案的编辑。就我而言,他不仅否决了答案,而且还将其从工作代码更改为非工作代码。
【解决方案4】:

这是在python中最简单的方法

from random import randint, choice

input_list = ['nu', 'ne', 'na', 'ku', 'ke', 'ka']


for x in range(50): (print "".join([choice(input_list) for x in range(randint(1,4))]))

【讨论】:

    猜你喜欢
    • 2013-10-19
    • 2017-03-26
    • 2023-01-11
    • 2021-12-30
    • 2011-05-29
    • 1970-01-01
    • 1970-01-01
    • 2012-01-12
    • 2018-03-25
    相关资源
    最近更新 更多