Python:SyntaxError:for的语法无效

2024-06-16 09:53:39 发布

您现在位置:Python中文网/ 问答频道 /正文

当我尝试用Python编译代码时,会收到这个错误消息。似乎它不喜欢我的“for”。你知道吗

File "flow.py", line 74

strat_improvements = {strategy: 0 for strategy in STRATEGIES}

                                    ^

SyntaxError: invalid syntax

有人能告诉我这是为什么吗?我对Python还比较陌生。这是一个代码块。。。你知道吗

def solve(data):
    """Solves an instance of the flow shop scheduling problem"""

    # We initialize the strategies here to avoid cyclic import issues                                                                                                                               
    initialize_strategies()
    global STRATEGIES

    # Record the following for each strategy:                                                                                                                                                       
    #  improvements: The amount a solution was improved by this strategy                                                                                                                            
    #  time_spent: The amount of time spent on the strategy                                                                                                                                         
    #  weights: The weights that correspond to how good a strategy is                                                                                                                               
    #  usage: The number of times we use a strategy                                                                                                                                                 
    strat_improvements = {strategy:
                          0 for strategy in STRATEGIES}
    strat_time_spent = {strategy: 0 for strategy in STRATEGIES}
    strat_weights = {strategy: 1 for strategy in STRATEGIES}
    strat_usage = {strategy: 0 for strategy in STRATEGIES}

    # Start with a random permutation of the jobs                                                                                                                                                   
    perm = range(len(data))
    random.shuffle(perm)

    # Keep track of the best solution                                                                                                                                                               
    best_make = makespan(data, perm)
    best_perm = perm
    res = best_make

Tags: oftheinfordatatimebeststrategies