You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何编写循环实现带数字序号的给定列表打印

Python带序号打印列表元素实现方案
  • 你可以直接使用Python内置的enumerate()函数完成需求,它能在遍历列表时同时返回元素的索引值和对应内容,只需要把默认从0开始的索引加1,就能得到符合要求的从1开始的序号。
  • 示例代码如下:
ingredients = ['snail', 'leeches']
for idx, food in enumerate(ingredients):
    print(f"{idx+1}  {food}")
  • 运行后输出结果完全符合要求:
1  snail
2  leeches

如果需要输出中文内容,只需要把列表里的食材换成对应的中文翻译即可:

ingredients = ['蜗牛', '水蛭']
for idx, food in enumerate(ingredients):
    print(f"{idx+1}  {food}")

运行后输出:

1  蜗牛
2  水蛭

内容的提问来源于stack exchange,提问作者Genesis Ibisola

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.28 21:45:04