有没有办法遍历列表并根据列表内容更改字符串?

时间:2021-02-11 04:02:53

标签: python for-loop

我是 Python 的新手,正在尝试边做边学,并开始了我的第一个真正的项目。甚至不到 4 小时,我就遇到了一个巨大的障碍。我正在尝试配对可能的输入变体,以便我可以使用该输入来引用字典。不知道这是否可能,并且无法使用参考资料弄清楚。如果可能的话,我什至可以远程关闭吗?

yeast_check = ['lalvin', 'zymaflore', 'actiflore', 'redstar', ' ', 'fermemtis', 'lallemand', 'safale', 'bourgoblanc', 'bourgorouge', 'k1','icv', '-']

yeast_input = input('enter yeast type:')

def yeast_convert(yeast_input):
    for check in list(yeast_check):
        yeast_input = yeast_input.replace(check, '')
        return yeast_input

1 个答案:

答案 0 :(得分:0)

yeast_check = ['lalvin', 'zymaflore', 'actiflore', 'redstar', ' ', 'fermemtis', 'lallemand', 'safale', 'bourgoblanc', 'bourgorouge', 'k1','icv', '-']

yeast_input = input('enter yeast type:')

def yeast_convert(yeast_input):
    for check in list(yeast_check):
        yeast_input = yeast_input.replace(check, '')
    return yeast_input

print(yeast_convert(yeast_input))

尝试在 for 循环之后调用它