方法在列表中用for循环

states = ["   Alabama ", "Georgia!", "Georgia", "georgia", "FlOrIda",
          "south   carolina##", "West virginia?"]

import re

def remove_punctuation(value):
    return re.sub("[!#?]", "", value)

clean_ops = [str.strip, remove_punctuation, str.title] # 注意str.strip和str.title的使用

def clean_strings(strings, ops):
    result = []
    for value in strings:
        for func in ops: # 注意这里把函数当作变量传递
            value = func(value)
        result.append(value)
    return result

clean_strings(states, clean_ops)

# 输出如下
['Alabama',
 'Georgia',
 'Georgia',
 'Georgia',
 'Florida',
 'South   Carolina',
 'West Virginia']

你可能感兴趣的:(numpy)