python - 如何在 Python 中请求一组列表,请求一个数字 (x),删除 x 个输入,然后重新列出这些输入?

标签 python list

我是 Python 新手,目前正在学习列表。这就是我想要解决的问题:

Your favourite band is in town, and tickets are selling fast! Alas, you were too late to snag one, so you put your name in the waitlist, in case any extra tickets are released.

Write a program to manage the waitlist for the concert.

Your program should read in a list of the names in the waitlist, and the number of extra tickets released.

Then, it should announce the names of people who score the extra tickets.

Here's an example of how your program should work:

People in line: Dave, Lin, Toni, Markhela, Ravi
Number of extra tickets: 3
Tickets released for: Dave, Lin, Toni

Note: The names are separated by a comma and a space (', ').

If there are no more tickets released, your program should work like this:

People in line: Mali, Micha, Mary, Monica
Number of extra tickets: 0
Fully Booked!

This band is so popular that there will always be at least as many people as extra tickets. You won't have to worry about index errors.

我尝试了以下方法,但它总是打印整个列表,而不仅仅是一个子集。

ppl = []
sep = ', '
ppl_in_line = input('People in line: ')
ppl.append(ppl_in_line)
x = int(input('Number of extra tickets: '))
if x == 0:
    print('Fully Booked!')
else:    
    y = ppl[:x]
    print('Tickets released for: ' + (sep.join(y)))

最佳答案

ppl_in_line 是一个字符串。因此,当您附加到 ppl 时,您将附加一个字符串。

要在一行中输入单独的 ppl 列表,请执行以下操作:

ppl_in_line = input('排队的人:').split(sep)

关于python - 如何在 Python 中请求一组列表,请求一个数字 (x),删除 x 个输入,然后重新列出这些输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60784096/

相关文章:

python - 按多个条件对列表进行排序并附加

python - 椭圆化列表连接,Pythonically

python - 如何在 yolo 暗网中找到每个类别的信心

java - 一次性收集 Java Stream 的平均值

python - Databricks:使用爆炸进行数据帧分割

python - pytest 和 codecov : no coverage report found

python - 如何通过用户输入创建一个字符串数组并在 Python 中按字典顺序打印它们?

list - Haskell 中的置换实现

python - 使实例中的选定属性成为只读的最简单、最简洁的方法是什么?

python - Django 模型将短语附加到 app_label