首页 > 解决方案 > 简单的嵌套 for 循环

问题描述

我是 Python 新手,需要以下代码方面的帮助。任务是用空字符串“”替换所有“坏”符号和字符。

test_data = [“1913-1923”,“(1951)”,“1994”,“1934”,“c. 1915”,“1995”,“(1988)”,“2002”,“1957-1959”, c. 1955.”、“c. 1970 年代”、“C. 1990-1999”]

bad_chars = ["(",")","c","C",".","s","'",""]

这是我认为可行的:

def func1(list1, tobereplaced):
    for x in tobereplaced:
        for i in list1:
            i = i.replace(x,'')
    return list1

该函数未对列表进行任何更改。有人可以向我解释发生了什么问题吗?非常感谢!

标签: pythonfor-loop

解决方案


推荐阅读