从多个元组计算 [英] Calculating from multiple tuples

查看:61
本文介绍了从多个元组计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取元组列表的列表,并将每个元组列表转换为单个元组.像这样:

I'm trying to take a list of lists of tuple and turn each list of tuples into a single tuple. Like this:

当前拥有:

[[("Erikson,Ann",2.0,3),("Erikson,Ann",3.33,3)],[("Lewis,Buck",2.66,1), 
  ("Lewis,Buck",2.0,3)],[("Smith,John",0.0,1),("Smith,John",1.66,3), 
  ("Smith,John",1.33,3)],[("Torvell,Sarah",4.0,3)]]

我希望表格是一个元组列表.每个人名字一个元组.除了将元组列表合并为一个元组外,我还希望使用每个元组的第二个和第三个元素来计算人的gpa.第二个数字是该课程的学分,第三个数字是该课程的学分.我需要做的是获取每个元组的学分总和*等级点,然后将该总和除以每个元组中所有学分的总和.

And I want the form to be a single list of tuples. One tuple for each persons name. Along with combining the list of tuples into a single tuple I want to use the second and third elements of each tuple to calculate the gpa of the person. The second number is the grade point for that class and the third number is the credits for the class. What I need to do is take the sum of credits * gradepoint for each tuple and then divide that sum by the sum of all the credits in each tuple.

到目前为止,这是行不通的...

What i have so far, that doesn't work is this...

     calcGPA :: MyType2 -> MyType2 -> (String, Double, Double)
     calcGPA (a,b,c) (d,e,f) = (a, ((b*(fromIntegral c))+(e*(fromIntegral 
     f))/(b+e)), 
     (b+e))

我要传递的位置是我在帖子顶部显示的列表的第一个列表.

Where i am passing in the first list of lists I show at the top of this post.

我要朝正确的方向解决这个问题.任何提示或帮助,将不胜感激.

Am I going in the right direction to solve this problem. Any tips or help would be appreciated.

谢谢

编辑

谢谢您的帮助!帮助我了解实际情况.我写了cumulativeSums函数,如下所示:

Thank you for the help! Helped me understand what was actually going on. I wrote the cumulativeSums fuction as follow:

     cumulativeSums :: (Double, Int) -> (String, Double, Int) -> (Double, 
     Int)
     cumulativeSums (a,b) (c,d,e) = (a+(d*e), b+e)

我对let上面的代码块感到困惑.这去哪儿了?我是否将它放在自己的函数中,称为元组列表传递?

I'm confused on the chunk of code you have above with the let. Where does this go? Do I put it in its own function that I call passing in the list of list of tuples?

谢谢

________________________________________________________________________________现在我正在尝试输出信用额

________________________________________________________________________________Now that im trying to output credits also

    calcGPA :: [(String, Double, Int)] -> (String, Double, Int)
    calcGPA grades = let name                        = (\ (name, _, _) -> 
    name) (head grades)
                         (name, weightedSum, sumOfWeights) = foldl 
    cumulativeSums (name, 0, 0) grades
                 gpa                         = weightedSum / sumOfWeights
                     in  (name, gpa, credits)

推荐答案

您需要遍历每个子列表,以便累积值.像这样:

You need to go over each sub-list so you can accumulate values. Something like this:

averageGdp :: [[(String, Double, Double)]] -> [(String, Double, Double)]
averageGdp = fmap f
  where
    f             = (,,) <$> fst . head <*> totalAvg <*> totalCredit 
    fst (a, _, _) = a
    totalCredit   = getSum . foldMap (\(_, _, c) -> pure c)
    total         = getSum . foldMap (\(_, b, c) -> pure $ b * c)
    totalAvg      = (/) <$> total <*>  totalCredit

f将内部列表作为其输入并产生一个三元组.然后,将f映射到外部列表上.

f takes the inner list as its input and produces a triple. You then map f over the outer list.

这篇关于从多个元组计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆