[英]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.