python中的编号列表 [英] Numbered list in python

查看:262
本文介绍了python中的编号列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从python中的列表元素中创建一个编号列表. 示例列表:

I need to make a numbered list from list elements in python. Example list:

destinations = ['Los Angeles ', 'Rhodos ', 'Dubai ', 'Manila ', 'Mallorca ', 'New York ']

我需要将元素打印为编号列表:

I need to print out elements as numbered list:

1. Los Angeles 
2. Rhodos 
3. Dubai 
4. Manila 
5. Mallorca 
6. New York

如果我这样做:

print ('\n'.join(destinations)) 

它在单独的行上打印出元素,但是我不能加数字.

It prints out elements on separate lines, but I cannot add numbers.

推荐答案

您只需使用 enumerate() 并从1

>>> destinations = ['Los Angeles ', 'Rhodos ', 'Dubai ', 'Manila ', 'Mallorca ', 'New York ']  
>>> for index, value in enumerate(destinations, 1):
...     print("{}. {}".format(index, value))
... 
1. Los Angeles 
2. Rhodos 
3. Dubai 
4. Manila 
5. Mallorca 
6. New York 

这篇关于python中的编号列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆