英文:
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'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**
['Joe', ' Frank', ' Janet', ' Dion', ' Rachel', ' Lilly', ' Alyx']
['Joe', ' Frank', ' Janet', ' Dion', ' Rachel', ' Lilly', ' Alyx']
['Joe', ' Frank', ' Janet', ' Dion', ' Rachel', ' Lilly', ' Alyx']
</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(',')]
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 [['Joe', ' Frank', ' Janet', ' Dion', ' Rachel', ' Lilly', ' Alyx']]
(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(',')
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(','), k=num), sep="\n")
or if you want to prevent duplicates:
def listgen(num):
print(*random.sample(oglist.split(','), num), sep="\n")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论