小编rha*_*ll1的帖子

从Haskell中的列表中打印元素

我正在尝试对列表进行排序,并检查我的排序算法是否正常工作,因为我希望我打印出排序列表中的特定元素,我希望这是一个简单的任务,但事实证明非常困难 - 我想我会以错误的方式去做.

data Candidate = Candidate Float Float Float String
Run Code Online (Sandbox Code Playgroud)

...

getName :: Candidate -> String
getName (Candidate weight profit effic name) = name
Run Code Online (Sandbox Code Playgroud)

...

main = do
    let items = [Candidate 0.20 4.17 (calculateEfficiency 0.20 4.17) "Weapon"]
    Candidate 3.11 4.53 (calculateEfficiency 3.11 4.53) "Tinned food":items
    Candidate 1.04 4.64 (calculateEfficiency 1.04 4.64) "Ammunition":items
    Candidate 2.70 1.19 (calculateEfficiency 2.70 1.19) "Water":items   

    let sortedItems = sortBy mySort items
    putStrLn (getName (sortedItems !! 0))
Run Code Online (Sandbox Code Playgroud)

我得到的错误是:

Couldn't match expected type `[b0]' with actual …
Run Code Online (Sandbox Code Playgroud)

printing sorting haskell list ghci

1
推荐指数
1
解决办法
158
查看次数

TypeError:在列表中使用'sum()'时,'NoneType'对象不可迭代

我正进入(状态

TypeError: 'NoneType' object is not iterable
Run Code Online (Sandbox Code Playgroud)

当我试图找到一个列表的总和.

出现问题的地方:

if(sum(self._candidates) + self._allCandidates[self._depth]._weight > 20):
    self._left = Node(self._candidates, self._depth + 1, self._allCandidates)
else:
    self._left = Node(self._candidates.append(self._allCandidates[self._depth]), self._depth + 1, self._allCandidates)
Run Code Online (Sandbox Code Playgroud)

节点定义:

def __init__(self, candidates = [], depth = -1, allCandidates = []):
        self._candidates = candidates
        self._depth = depth
        self._allCandidates = allCandidates
Run Code Online (Sandbox Code Playgroud)

感谢您对此事的任何帮助.

python sum list nonetype

0
推荐指数
1
解决办法
728
查看次数

标签 统计

list ×2

ghci ×1

haskell ×1

nonetype ×1

printing ×1

python ×1

sorting ×1

sum ×1