创建一个从字符串中生成列表的方法,然后从该列表中随机选择(x)次。

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

How to take a string, create a list from string, and select randomly from that list (x) times

问题

以下是翻译好的部分:

# 我想创建一个Python函数,它将接受用逗号分隔的名称作为输入;将该字符串转换为列表,然后从该列表中选择(num-参数)个名称。

我正在使用random.choice(list)但函数不是打印列表中的num个名称而是简单地多次打印列表本身num我无法弄清楚我做错了什么这是我在Stack Overflow上的第一个问题
- 我正在导入random模块
- 我正在使用split以逗号分隔名称
- num参数应该帮助随机选择名称num

以下是我的代码和输出

```python
import random

def listgen(num):
    newlist = oglist.split(',')
    for x in range(num):
        print(random.choice(newlist))

print("Type in your list. Separate each name by a comma and press enter when finished")
oglist = input()

print("how many random names do you need?")
num = int(input())

listgen(num)

输出:

输入您的列表。用逗号分隔每个名称,完成后按回车键

Joe, Frank, Janet, Dion, Rachel, Lilly, Alyx

您需要多少个随机名称?

3

['Joe', ' Frank', ' Janet', ' Dion', ' Rachel', ' Lilly', ' Alyx']

['Joe', ' Frank', ' Janet', ' Dion', ' Rachel', ' Lilly', ' Alyx']

['Joe', ' Frank', ' Janet', ' Dion', ' Rachel', ' Lilly', ' Alyx']


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

# I want to create a Python function which will take names as input separated by commas; turn that string into a list, and finally choose (num - parameter) number of names from that list. 

I am using random.choice(list), but instead of the function printing out (num) number of names from the list, the list itself is simply being printed (num) number of times instead. I can&#39;t figure out what I am doing wrong. This is my first ever question on Stack Overflow. 
- I am importing random module
- I am using split to delineate names by commas
- (num) parameter should help print randomly chosen names (num) times

Here is my code and also the output:

import random

def listgen(num):
newlist=[oglist.split(',')]
for x in range(num):
print(random.choice(newlist))

print("Type in your list. Separate each name by a comma and press enter when finished")
oglist=input()

print("how many random names do you need?")
num=int(input())

listgen(num)


---
**Output:**

Type in your list. Separate each name by a comma and press enter when finished

**Joe, Frank, Janet, Dion, Rachel, Lilly, Alyx**

how many random names do you need?

**3**

[&#39;Joe&#39;, &#39; Frank&#39;, &#39; Janet&#39;, &#39; Dion&#39;, &#39; Rachel&#39;, &#39; Lilly&#39;, &#39; Alyx&#39;]

[&#39;Joe&#39;, &#39; Frank&#39;, &#39; Janet&#39;, &#39; Dion&#39;, &#39; Rachel&#39;, &#39; Lilly&#39;, &#39; Alyx&#39;]

[&#39;Joe&#39;, &#39; Frank&#39;, &#39; Janet&#39;, &#39; Dion&#39;, &#39; Rachel&#39;, &#39; Lilly&#39;, &#39; Alyx&#39;]


</details>


# 答案1
**得分**: 2

newlist = oglist.split(',')  # 创建一个以逗号分隔的新列表
说:“创建一个包含分割后的内容的`list`,并将该`list`包装在一个包含分割后的`list`作为唯一元素的外部`list`中。” `newlist` 最终变成 `[['Joe', ' Frank', ' Janet', ' Dion', ' Rachel', ' Lilly', ' Alyx']]`(注意额外的外部方括号)。 然后,`random.choice` 从一个元素的外部`list`中“随机”选择,每次都返回内部`list`。 去掉外部方括号,以获得一个可以选择的单层`list`:

```python
newlist = oglist.split(',')

作为附注,现代Python可以为您选择多个项目,而不需要编写自己的循环,将listgen 简化为以下之一:

def listgen(num):
    print(*random.choices(oglist.split(','), k=num), sep="\n")

或者,如果要防止重复:

def listgen(num):
    print(*random.sample(oglist.split(','), num), sep="\n")
英文:
newlist=[oglist.split(&#39;,&#39;)]

says "Make a list of the split up contents, and wrap that list in an outer list containing the split up list as its only element". newlist ends up being [[&#39;Joe&#39;, &#39; Frank&#39;, &#39; Janet&#39;, &#39; Dion&#39;, &#39; Rachel&#39;, &#39; Lilly&#39;, &#39; Alyx&#39;]] (note extra set of outer brackets). random.choice then "randomly" selects from the one-element outer list, returning the inner list every time. Get rid of the outer brackets to have a single layer list to choose from:

newlist = oglist.split(&#39;,&#39;)

As a side-note, modern Python can pick multiple items for you without writing your own loop, simplifying listgen to one of:

def listgen(num):
    print(*random.choices(oglist.split(&#39;,&#39;), k=num), sep=&quot;\n&quot;)

or if you want to prevent duplicates:

def listgen(num):
    print(*random.sample(oglist.split(&#39;,&#39;), num), sep=&quot;\n&quot;)

huangapple
  • 本文由 发表于 2023年3月1日 10:26:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75599057.html
匿名

发表评论

匿名网友

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

确定