从一个包含 n 个数字的列表中选择样本,不重复。

huangapple go评论107阅读模式
英文:

Selecting sample from a list of n numbers without repeating

问题

以下是您要翻译的代码部分:

num_list = ["3", "2", "5", "6", "9", "3", "0"]

请注意,这是您提供的Python代码的翻译部分。

英文:

So lets say I have a python list:

num_list = ["3", "2", "5", "6", "9", "3", "0"]

What I want to do is:
each time select a sample with 5 numbers from this list;
the first number can only be 0, 3, or 9.

How can I write an algorithm in python find all the combinations that can be sampled from the list with the above two conditions?

I have used the random module in python, but it can't do restricted sample selection like the condition for first number being only 0, 3 or 9.

答案1

得分: 0

create a for loop with elements = ['0', '3', '9']
then make a variable x = [element]
then use random.choice as usual

combinations = []
elements = ['0', '3', '9']
for element in elements:
    x = [element]
    while len(x) < 5:
        x.append(random.choice(num_list))
    else:
        combinations.append(x)
    # you need to make the part under for loop run till all combinations
    # are stored
    # you also need to remove the current element from the list or else
    # it will be considered twice, to do this make a copy of list and 
    # remove the current element in the for loop
英文:

create a for loop with elements = ['0', '3', '9']
then make a variable x = [element]
then use random.choice as usual

    num_list = ['3','2','5','6','9','3','0']
    combinations = []
    elements = ['0', '3', '9']
    for element in elements:
        x = [element]
        while len(x) < 5:
            x.append(random.choice(num_list))
        else:
            combinations.append(x)
        # you need to make the part under for loop run till all combinations
        # are stored
        # you also need to remove the current element from the list or else
        # it will be considered twice, to do this make a copy of list and 
        # remove the current element in the for loop

答案2

得分: 0

Sure, here's the translated code part:

如果你的任务是完全自己设计算法你可以迭代处理排列并在每次迭代中根据第一个元素在检查列表中的条件进行筛选

如果你的任务允许使用内置模块我建议你看看Python的 `itertools`。在你的情况下使用 `permutations` 函数

```python
from itertools import permutations

num_list = ['3', '2', '5', '6', '9', '3', '0']
# 这里的 permutations(num_list, 5) 很重要,其中 5 是样本的大小
result = list(filter(lambda n: n[0] in ('0', '3', '9'), permutations(num_list, 5)))

print(len(result)) #1440

print(*result[:6], sep='\n') # 仅列出前 6 个结果
# ('3', '2', '5', '6', '9')
# ('3', '2', '5', '6', '3')
# ('3', '2', '5', '6', '0')
# ('3', '2', '5', '9', '6')
# ('3', '2', '5', '9', '3')
# ('3', '2', '5', '9', '0')

<details>
<summary>英文:</summary>

If your assignment is to come up with the algorithm by yourself completely, you could iterate over the permutations and filter on every iteration by the condition of the first element being in the checklist.

If your assignment accepts using built-in modules, I recommend you to take a look at the python `itertools`. In your case, the `permutations` function.

    from itertools import permutations

    num_list = [&#39;3&#39;,&#39;2&#39;,&#39;5&#39;,&#39;6&#39;,&#39;9&#39;,&#39;3&#39;,&#39;0&#39;]
    # permutations(num_list, 5) is the important here, where 5 is the size of the sample
    result = list(filter(lambda n: n[0] in (&#39;0&#39;,&#39;3&#39;,&#39;9&#39;), permutations(num_list, 5)))

    print(len(result)) #1440

    print(*result[:6], sep=&#39;\n&#39;) # listing only the first 6 results
    # (&#39;3&#39;, &#39;2&#39;, &#39;5&#39;, &#39;6&#39;, &#39;9&#39;)
    # (&#39;3&#39;, &#39;2&#39;, &#39;5&#39;, &#39;6&#39;, &#39;3&#39;)
    # (&#39;3&#39;, &#39;2&#39;, &#39;5&#39;, &#39;6&#39;, &#39;0&#39;)
    # (&#39;3&#39;, &#39;2&#39;, &#39;5&#39;, &#39;9&#39;, &#39;6&#39;)
    # (&#39;3&#39;, &#39;2&#39;, &#39;5&#39;, &#39;9&#39;, &#39;3&#39;)
    # (&#39;3&#39;, &#39;2&#39;, &#39;5&#39;, &#39;9&#39;, &#39;0&#39;)


</details>



# 答案3
**得分**: 0

不清楚你是否想要该列表的所有可能排列,或者只是从随机选择中获取所有排列。也就是说,你是否可以从列表中多次抽取相同的数字,还是在选择时数字基本上是从列表中弹出的。

假设你需要以0、3或9开头的长度为5的每个[组合](https://docs.python.org/3/library/itertools.html#itertools.combinations)。

```python
from itertools import combinations

num_list = ["3", "2", "5", "6", "9", "3", "0"]

c = combinations(num_list, 5)
candidates = [l for l in c if l[0] in ["0", "3", "9"]]

这将生成一个包含15个项目的 candidates 列表。

如果你希望获得该列表的所有可能长度为5的排列

from itertools import permutations

num_list = ["3", "2", "5", "6", "9", "3", "0"]

p = permutations(num_list, 5)
candidates = [l for l in p if l[0] in ["0", "3", "9"]]

这将生成一个包含1440个项目的 candidates 列表,因为它允许元素重复。

所以这些方法可以让你找到所有可能的排列,如果你 需要 找到所有可能的排列。但如果你需要随机选择而不是找到所有可能的排列,那就没有必要浪费内存和时间在列表上,直接从itertools对象中获取,因为它们是生成器,会根据需要生成

英文:

It isn't clear if you want all possible arrangements of that list of or truly all permutations from a random selection. That is, can you sample the same number from the list twice or are the numbers essentially being popped of the list at selection time.

Let's assume you need every combination of length 5 where 0, 3, or 9 begin the list.

from itertools import combinations

num_list = [&quot;3&quot;, &quot;2&quot;, &quot;5&quot;, &quot;6&quot;, &quot;9&quot;, &quot;3&quot;, &quot;0&quot;]

c = combinations(num_list, 5)
candidates = [l for l in c if l[0] in [&quot;0&quot;, &quot;3&quot;, &quot;9&quot;]]

This yields a list of candidates that is 15 items long.

If instead you want all possible length 5 permutations of that list.

from itertools import permutations

num_list = [&quot;3&quot;, &quot;2&quot;, &quot;5&quot;, &quot;6&quot;, &quot;9&quot;, &quot;3&quot;, &quot;0&quot;]

p = permutations(num_list, 5)
candidates = [l for l in p if l[0] in [&quot;0&quot;, &quot;3&quot;, &quot;9&quot;]]

That yields a list of canbidates that is 1440 items long as it allows for repeated elements.


So these allow you to find all possible, if you need to find all possible. But if you need to do so randomly without finding all possible, no sense wasting the memory and time on list, just pull them from the itertools objects directly as they are generators that will yield as required.

答案4

得分: 0

不需要从所有组合中进行抽样(随着物品数量的增加,这将很快变得不切实际)。相反,将第一项的选择与其余部分的抽样分开。为此,请从包含0、3或9的索引的随机选择中选择第一个数字。然后从剩余位置中选择4个样本。

nums = ["3", "2", "5", "6", "9", "3", "0"]

import random

firstIndexes = [i for i, n in enumerate(nums) if n in "039"]
for _ in range(10):
   i0 = random.choice(firstIndexes)
   result = (nums[i0], *random.sample(nums[:i0] + nums[i0 + 1:], 4))
   print(result)

输出如下:

('9', '2', '0', '6', '5')
('3', '0', '6', '2', '9')
('9', '5', '3', '3', '2')
('3', '0', '9', '3', '5')
('9', '0', '2', '3', '6')
('3', '6', '5', '2', '3')
('0', '9', '3', '3', '6')
('3', '2', '3', '9', '6')
('9', '3', '6', '2', '3')
('9', '5', '0', '2', '3')
英文:

You don't need to sample from all the combinations (which would quickly become prohibitive as you increase the number of items). Instead, separate the selection of the first item from sampling of the rest. To do so, select the first number from a random choice of the indexes that contain 0, 3 or 9. Then pick a sample of 4 from the remaining positions

nums = [&quot;3&quot;, &quot;2&quot;, &quot;5&quot;, &quot;6&quot;, &quot;9&quot;, &quot;3&quot;, &quot;0&quot;]

import random

firstIndexes = [i for i,n in enumerate(nums) if n in &quot;039&quot;]
for _ in range(10):
   i0 = random.choice(firstIndexes)
   result = (nums[i0],*random.sample(nums[:i0]+nums[i0+1:],4))
   print(result)

(&#39;9&#39;, &#39;2&#39;, &#39;0&#39;, &#39;6&#39;, &#39;5&#39;)
(&#39;3&#39;, &#39;0&#39;, &#39;6&#39;, &#39;2&#39;, &#39;9&#39;)
(&#39;9&#39;, &#39;5&#39;, &#39;3&#39;, &#39;3&#39;, &#39;2&#39;)
(&#39;3&#39;, &#39;0&#39;, &#39;9&#39;, &#39;3&#39;, &#39;5&#39;)
(&#39;9&#39;, &#39;0&#39;, &#39;2&#39;, &#39;3&#39;, &#39;6&#39;)
(&#39;3&#39;, &#39;6&#39;, &#39;5&#39;, &#39;2&#39;, &#39;3&#39;)
(&#39;0&#39;, &#39;9&#39;, &#39;3&#39;, &#39;3&#39;, &#39;6&#39;)
(&#39;3&#39;, &#39;2&#39;, &#39;3&#39;, &#39;9&#39;, &#39;6&#39;)
(&#39;9&#39;, &#39;3&#39;, &#39;6&#39;, &#39;2&#39;, &#39;3&#39;)
(&#39;9&#39;, &#39;5&#39;, &#39;0&#39;, &#39;2&#39;, &#39;3&#39;)

答案5

得分: -2

一种方法是使用一个简单的循环来迭代,直到获得其中一个选定的数字:

number = None
while True:
    number = random.choice(arr)
    if number in [0, 3, 9]:
        break

然后像往常一样使用 random.choice 来获取其他数字。

英文:

One way would be to use a simple loop to iterate until you get one of the selected numbers:

number = None
while True:
    number = random.choice(arr)
    if number in [0, 3, 9]:
        break

Then use the random.choice as usual for the rest of the numbers.

huangapple
  • 本文由 发表于 2023年3月21日 03:20:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75794453.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定