简体   繁体   English

当我打印清单上的项目时,如何编号?

[英]How would I number the items on my list out when I print them?

I am redoing an assignment I have not gotten a perfect score on and I seem to not understand how to label my list with numbers, also trying to print it on the same line. 我正在重做一项尚未得到满分的作业,而且我似乎不明白如何用数字标记清单,并且尝试将其打印在同一行上。

I've tried adding numbers into my list so categorize but that it was not my mentor wants. 我尝试将数字添加到列表中,以便进行分类,但这并不是我的导师想要的。 She is asking for me number the items in the list using a for loop counter. 她要我使用for循环计数器为列表中的项目编号。

games = ["Super Mario Maker 2", "Bayonetta 3", "Dark Souls 4", "Astral Chain", "Persona 5 Royale"]

for n in range(0, len(games)):
    print("Game: " + games[n])

Currently this prints out everything in the list from star to finish. 目前,这会打印出列表中从星号到结尾的所有内容。 It starts out with Game: (1st item in list) and so forth. 它以Game :(列表中的第一项)开始,依此类推。 I'm trying to figure out how to label these as they go forth so that it would print something like Game 1: (1st item), Game 2: (2nd item) and so forth, printing everything on a single line with commas in between. 我试图弄清楚如何在它们前进时给它们加标签,以便它能打印出类似Game 1: (1st item), Game 2: (2nd item)并用逗号将所有内容打印在一行上之间。

With enumerate : enumerate

for idx, val in enumerate(games): 
      print('{}: {}'.format(idx, val))
# Out:
# Game 0: Super Mario Maker 2
# Game 1: Bayonetta 3
# Game 2: Dark Souls 4
# Game 3: Astral Chain
# Game 4: Persona 5 Royale

Or using the enumerate option start : 或使用enumerate选项start

for idx, val in enumerate(games, start=1): 
    print('{}: {}'.format(idx, val))
# Out:
# Game 1: Super Mario Maker 2
# Game 2: Bayonetta 3
# Game 3: Dark Souls 4
# Game 4: Astral Chain
# Game 5: Persona 5 Royale

In one line: 一行:

print(*['Game {}: {}'.format(i, v) for i,v in enumerate(games, start=1)], sep='\n')  

Use enumerate : 使用enumerate

for counter, value in enumerate(some_list):
    print(counter, value)

So, for your example: 因此,以您的示例为例:

for i, game in enumerate(games):
   print("Game {0}: {1}".format(i+1, game))

For Python 3.6 or later: 对于Python 3.6或更高版本:

for i, game in enumerate(games):
    print(f"Game {i + 1}: {game}")

As you can also see, f-strings are very useful and come in handy for what you're trying to do! 正如您还看到的那样,f字符串非常有用,可以派上用场!

EDIT: printing as a single line: 编辑:打印为单行:

print(', '.join([f"Game {i + 1}: {game}" for i, game in enumerate(games)]))

You can print all items on a list using join and format string based on the index of each item in the list. 您可以根据列表中每个项目的索引使用连接和格式字符串在列表中打印所有项目。

', '.join(f'Game {i+1}: {games[i]}' for i in range(len(games)))

Result: 结果:

'Game 1: Super Mario Maker 2, Game 2: Bayonetta 3, Game 3: Dark Souls 4, Game 4: Astral Chain, Game 5: Persona 5 Royale'

Add n to the output. 将n添加到输出中。

for n in range(0, len(games)): 
    print("Game {}: {}".format(n, games[n]))

It's actually really simple, you can just use: 实际上非常简单,您可以使用:

  • enumerate with the optional start argument to get the indexes starting from 1 用可选的start参数enumerate以获得从1开始的索引

  • a generator expression to loop over the values of the list 生成器表达式,用于遍历列表的值

  • an f-string to insert the variables to a string f字符串 ,将变量插入字符串

  • str.join to concatenate the elements to one string with commas in between str.join将元素连接到一个字符串,中间用逗号隔开

games = ["Super Mario Maker 2", "Bayonetta 3", "Dark Souls 4", "Astral Chain", "Persona 5 Royale"]

print(', '.join(f'Game {i}: {game}' for i, game in (enumerate(games, start=1))))

Output: 输出:

 Game 1: Super Mario Maker 2, Game 2: Bayonetta 3, Game 3: Dark Souls 4, Game 4: Astral Chain, Game 5: Persona 5 Royale 

add this after game in the string 在游戏后在字符串中添加

str(n+1) 

so it should be like this 所以应该是这样

'game '+str(n+1) +' rest of the string'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何在 python 中将任何数字 x 次打印为列表? - How would I print out any number x times as a list in python? 如果最大的数字在同一个列表中多次出现,我如何将它们全部打印出来? - If the largest number occurs more than once in the same list, how do I print them all out? 我如何从一行文本的开头获取数字,将它们拆分并打印出来 - How would I get numbers from the beginning of a line of text, split them and print them out 如何打印字典中的选定项目? - How do I print out select items from my dictionary? 如何在列表和字典中打印带有“for loops”的项目数? - How do I print the number of items with 'for loops' in a list and dictionary? 我如何确定一个列表中的项目是否大于另一个列表中的项目? - How would I find out if items in one list are greater than items in another list? 我如何在 python 中打印出完美数字及其旁边显示的除数? - How would I print out the perfect numbers along with their divisors shown next to them in python? 当我在范围内打印出来时,我的列表是空的 - My list is empty when i print it out of for in range 如何减去库存中的物品并稍后在我的程序中打印它们? - How do I subtract items that are in my inventory and print them later in my program? 如何打印列表中的单个单词? - How do I print out individual words within my list?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM