【问题标题】:Attempting to receive a list of tuples but somewhat confused尝试接收元组列表但有些困惑
【发布时间】:2015-03-31 23:51:10
【问题描述】:

我正忙于通过麻省理工学院的开放课件论文,并且有点卡在某个问题上。

问题涉及通过在文本中的不同点多次移动来解决具有多级加密的凯撒密码。

我编写的代码解决了这些移位问题,并返回了解密凯撒移位文本所需的位置和移位编号。

该函数应返回一个包含班次开始位置和班次编号的元组列表。目前我得到的只是以下内容:

(([12, 11], [3, 9]), [0, 21])

任何帮助将不胜感激。

def find_best_shifts_rec(wordlist, text, start)

    for shift in xrange(27):
        ## Begin shifting the text along the segments you would like to see shifted while holding constant the text that is not to change.
        s = text[:start] + apply_shift(text[start:],shift)
        print s
        letter_position = start
        for letter in s[start:]:
            if letter == " " and is_word(wordlist,s[start:letter_position]):
                start = letter_position + 1
            letter_position += 1
        if is_word(wordlist,s[start:len(s)]):
            print "Base Case"
            shifts += (begin,shift)
            return shifts
        elif start != begin:
            print "Recursion"
            shifts += (begin,shift)
            return find_best_shifts_rec(wordlist,s,start), shifts

【问题讨论】:

    标签: python list recursion tuples


    【解决方案1】:

    问题似乎是你代码的最后一行

    return find_best_shifts_rec(wordlist,s,start), shifts
    

    问题是 find_best_shifts_rec 应该返回 listtuples,但您正在使用此结果生成 tuple。这可以通过将行更改为来解决:

    return find_best_shifts_rec(wordlist,s,start) + shifts
    

    这会将find_best_shifts_rec 返回的tupleslist 连接到shiftstupleslist

    【讨论】:

    • 啊,在这上面花了这么多时间,这就是我的问题。非常感谢。我必须做的唯一其他更改是更改:shifts += (begin,shift)shifts += [(begin,shift)]
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-29
    • 2018-12-03
    • 2023-03-30
    相关资源
    最近更新 更多