python 查找列表中的元素并替换

# # 查找列表中的元素并替换
# # 方法一
# lst = [1, 5, 3, 20, 6, 2, 7]
# for i in lst:
# if i % 2 == 0:
# lst[lst.index(i)] = 'even'
# print(lst)

# # 方法二
# lst = [1, 5, 3, 20, 6, 2, 7]
# for i in range(len(lst)):
# if lst[i] % 2 == 0:
# lst[i] = 'even'
# print(lst)

# # 方法三
# lst = [1, 5, 3, 20, 6, 2, 7]
# for i, ele in enumerate(lst):
# if ele % 2 == 0:
# lst[i] = 'even'
# print(lst)
    原文作者:guofeng80
    原文地址: https://blog.csdn.net/jingxinguofeng/article/details/117251577
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞