您当前的位置: 首页 >  Python

江湖有缘

暂无认证

  • 0浏览

    0关注

    446博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Python列表之for循环应用

江湖有缘 发布时间:2021-06-22 10:57:38 ,浏览量:0

Python列表之for循环应用
  • 一、程序要求及目的
  • 二、代码内容
  • 三、代码运行

一、程序要求及目的

将列表中姓张的人名元素改为姓李的,使用for循环

二、代码内容
# 列表在for循环中使用
lst = ["张飞", "赵云", "徐晃", "于禁", "张辽", "王平"]
for i in range(len(lst)):
    item = lst[i]
    if item.startswith("张"):
        new_name = "李"+item[1:]
        print(new_name)
        lst[i] = new_name

print(lst)

三、代码运行
D:\soft\python\python.exe D:/soft/pycharm/pycharmfile/py基础/02_python基础类型/10_列表的应用.py
李飞
李辽
['李飞', '赵云', '徐晃', '于禁', '李辽', '王平']

Process finished with exit code 0
关注
打赏
1665849170
查看更多评论
立即登录/注册

微信扫码登录

0.0407s