我如何在还涉及函数调用的单个列表理解中编写以下 python 代码?

How can i write the below python code in a single list comphrension which also involves a function call?

states = { 1:['Alabama@ ', 'Georgia!', 'Geor%gia%%', 'georgia', 'FlOrIda', 'southcarolina##', 'West virginia?']}

def remove_punctuations(value):
    return re.sub('[#!?@%]','',value) # remove these punctuations 

for_strings = [str.title, remove_punctuations, str.strip] # perform these  actions on strings 

def clean_strigs(strings,options):
    result = []
    for val in strings:
        #print(val)
        for function in options:
            val = function(val)
        result.append(val)
    return result

filter_dictonary(states[1],for_strings)

output = ['Alabama',
 'Georgia',
 'GeorGia',
 'Georgia',
 'Florida',
 'Southcarolina',
 'West Virginia']

我正在尝试编写 clean_string 函数并尝试在其中调用 for_list 但我无法这样做 我尝试了以下代码

def filter_column(strings,for_strings):
    result =  [val for value in strings for function  in for_strings for val in function(val) ]
    return result

有人能帮我写这个吗?

总结:就把上面的clean_strigs写在一个单表理解

理解力不适合手头的任务。这解决了它:

def filter_dictonary(strings,for_strings):
    for f in for_strings:
        strings = map(f,strings)
    return list(strings)

如果您通过执行此操作修复函数中的这个特定错误:

result =  [val for value in strings 
           for function in for_strings 
           for val in function(value) ]  # function(val) creates the error

它不会工作,因为您然后对单个字符进行操作,并且没有得到您要查找的函数的组合。

您可以使用以下解决方案。应该可以。

result = [reduce(lambda a, func: func(a), [value] + for_strings) for value in states[1] ]

列表理解中嵌套for循环的问题是前一个函数的结果不会传播到下一个函数。这就是 reduce 发挥作用的地方。

希望对您有所帮助。