Python 函数,该函数获取二维列表并查找列的最大值

Python 函数,该函数获取二维列表并查找列的最大值,python,python-3.x,list,recursion,Python,Python 3.x,List,Recursion,我正在尝试编写一个函数,它接受一个2d列表,查找列表中列中值最高的部分,然后返回列表中包括行和列的部分 这是一道模拟拼字的家庭作业题。本质上,给函数一个字符列表,它返回一个基于该单词的值和值本身的字符列表 在这个问题中有一些全局变量:scrabbleScores(该字符及其值的列表)和Dictionary(所有您可能生成的单词) 例如,如果您给它一个列表,['a',s',m',t',p'],它将首先调用另一个函数,返回您可以从该列表中生成的所有单词及其值,然后它将使用该列表并返回一个包含最佳单词

我正在尝试编写一个函数,它接受一个2d列表,查找列表中列中值最高的部分,然后返回列表中包括行和列的部分

这是一道模拟拼字的家庭作业题。本质上,给函数一个字符列表,它返回一个基于该单词的值和值本身的字符列表

在这个问题中有一些全局变量:scrabbleScores(该字符及其值的列表)和Dictionary(所有您可能生成的单词)

例如,如果您给它一个列表,['a',s',m',t',p'],它将首先调用另一个函数,返回您可以从该列表中生成的所有单词及其值,然后它将使用该列表并返回一个包含最佳单词及其值的列表

所以,我已经有了一个我一直在研究的函数,但每当我运行它时,我的IDE都会给我一个“int object is not subscriptable”错误:

scoreList()返回“[[a',1],“am',4],“at',2],“spam',8]]”,而bestWord()应该返回“['spam',8]”,但它还是不断地给我这个错误

如果你们能给我一些建议,我将不胜感激。最后,如果您提供了一个解决方案,那么它应该递归地工作/不使用循环。无论出于什么原因,我们都不允许在这个任务中使用循环。
另外,我知道我的'scoreList()'函数有一个循环,但我稍后会对它进行更改,看看我是否可以使用递归来工作。现在,不要理会我的虚伪P

代码很复杂,但我发现很少有错误,这些错误解决了错误带来的问题

bestWordHelper()
需要2D列表,但问题是在某些地方和列表元素之间存在
[]
,所以有时它会创建1D列表而不是2D列表,然后当您使用
L[0][1]
时,它无法获得第二维度,最后您得到的是
整数[0]
,而不是
列表[0]
因此您会收到错误消息“int对象不可订阅”


第一:您在中忘记了
[1]

 return [L[0][1], L[0][0]]

正如我所说的,
bestWordHelper()
需要2D列表,但要保持一致

 return bestWordHelper(bestWordHelper(L[0:1]) + bestWordHelper(L[1]))
您有
L[1]
,它是1D列表。应该有相当多的
L[1:][/code>

同一行中的另一个问题是
+
,它连接到1D列表并创建1D列表,但它必须创建2D列表

 return bestWordHelper( [bestWordHelper(L[0:1]), bestWordHelper(L[1L])] )

最后是代码(使用一些
print()
获取调试信息)

我还将list
scrabbleScores
转换为dictionary,因为它在这段代码中更有用。有了字典,获得
letterScore
wordScore

scrabbleScores = [['a', 1], ['m', 3], ['p', 3], ['s', 1], ['t', 1]]

scrabbleScores = dict(scrabbleScores)
print(scrabbleScores)
#scrabbleScores = {'a': 1, 'm': 3, 'p': 3, 's': 1, 't': 1}

Dictionary = ['a', 'am', 'at', 'apple', 'bat', 'bar', 'babble', 'can', 'foo',
              'spam', 'spammy', 'zzyzva']

#implementation missing because it works just fine, so just ignore this function
def letterScore(letter, scoreList):
    ''' letter = string; scoreList = list in the form of [character, value]
        returns the value associated with letter in scoreList '''
    return scoreList[letter]

#implementation missing because it works just fine, so just ignore this function
def wordScore(S, scoreList):
    ''' S = string; scoreList = list in the form of [character, value]
        returns the sum of the values for each character in S '''
    return sum(scoreList[letter] for letter in S)

def scoreList(Rack):
    ''' Rack = list of lower-case characters
    returns a list of all of the words in the global Dictionary that can be made
    from those letters and the score for each word '''
    def scoreListHelper(Rack, Dictionary):
        if Dictionary == []:
            return []
        elif all(c in Rack for c in Dictionary[0]):
            return [[Dictionary[0], wordScore(Dictionary[0], scrabbleScores)]] + scoreListHelper(Rack, Dictionary[1:])
        else:
            return scoreListHelper(Rack, Dictionary[1:])
    return scoreListHelper(Rack, Dictionary)

#this is the function that's having issues
def bestWord(Rack):
    ''' Rack = list of lower-case characters
    returns a list of the highest possible scoring word from Rack and the score 
    of the word '''
    def bestWordHelper(L):
        print('input:', L)
        if not L:
            print('empty')
            return []
        elif len(L) == 1:
            print('one:', L, '->', [L[0][1], L[0][0]])
            return [L[0][1], L[0][0]]
        else:
            print('many:', L)
            if L[1][1] > L[0][1]:
                return bestWordHelper(L[1:])
            else:
                return bestWordHelper([bestWordHelper(L[0:1]), bestWordHelper(L[1:])])

    result = scoreList(Rack)
    print('result:', result)
    return bestWordHelper(result)

print(bestWord(['a', 's', 'm', 't', 'p']))

错误显示哪一行出现问题,因此请使用
print()
查看此行变量中的值。变量中似乎只有一个整数值,但您将其视为列表运行ie.
5[1]
并收到相同的错误消息。好的,这似乎工作得很好。非常感谢你!
scrabbleScores = [['a', 1], ['m', 3], ['p', 3], ['s', 1], ['t', 1]]

scrabbleScores = dict(scrabbleScores)
print(scrabbleScores)
#scrabbleScores = {'a': 1, 'm': 3, 'p': 3, 's': 1, 't': 1}

Dictionary = ['a', 'am', 'at', 'apple', 'bat', 'bar', 'babble', 'can', 'foo',
              'spam', 'spammy', 'zzyzva']

#implementation missing because it works just fine, so just ignore this function
def letterScore(letter, scoreList):
    ''' letter = string; scoreList = list in the form of [character, value]
        returns the value associated with letter in scoreList '''
    return scoreList[letter]

#implementation missing because it works just fine, so just ignore this function
def wordScore(S, scoreList):
    ''' S = string; scoreList = list in the form of [character, value]
        returns the sum of the values for each character in S '''
    return sum(scoreList[letter] for letter in S)

def scoreList(Rack):
    ''' Rack = list of lower-case characters
    returns a list of all of the words in the global Dictionary that can be made
    from those letters and the score for each word '''
    def scoreListHelper(Rack, Dictionary):
        if Dictionary == []:
            return []
        elif all(c in Rack for c in Dictionary[0]):
            return [[Dictionary[0], wordScore(Dictionary[0], scrabbleScores)]] + scoreListHelper(Rack, Dictionary[1:])
        else:
            return scoreListHelper(Rack, Dictionary[1:])
    return scoreListHelper(Rack, Dictionary)

#this is the function that's having issues
def bestWord(Rack):
    ''' Rack = list of lower-case characters
    returns a list of the highest possible scoring word from Rack and the score 
    of the word '''
    def bestWordHelper(L):
        print('input:', L)
        if not L:
            print('empty')
            return []
        elif len(L) == 1:
            print('one:', L, '->', [L[0][1], L[0][0]])
            return [L[0][1], L[0][0]]
        else:
            print('many:', L)
            if L[1][1] > L[0][1]:
                return bestWordHelper(L[1:])
            else:
                return bestWordHelper([bestWordHelper(L[0:1]), bestWordHelper(L[1:])])

    result = scoreList(Rack)
    print('result:', result)
    return bestWordHelper(result)

print(bestWord(['a', 's', 'm', 't', 'p']))