Numbered list in python

后端 未结 2 946
情歌与酒
情歌与酒 2021-01-23 06:40

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

destinations = [\'Los Angeles \', \'Rhodos \', \'Dubai \', \'Manila \', \'Mallorca \'         


        
相关标签:
2条回答
  • 2021-01-23 07:05
    for i, dest in enumerate(destinations, 1):
        print(" %d. %s" % (i, dest))
    
    0 讨论(0)
  • 2021-01-23 07:12

    You simply use enumerate() and count from 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 
    
    0 讨论(0)
提交回复
热议问题