处理元组列表 [英] Working with list of tuples

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

问题描述

我一直在试图解决这个问题,但我无法弄清楚。例如:

  [(Mary,10),(John ,45),(Bradley,30),(Mary,15),(John,10)] 

以及我想得到的是一个带有元组的列表,如果名称相同,则应添加这些元组的数量,如果不是,则该元组必须是最终列表的一部分例如:

  [(Mary,25),(John,55),(Bradley, 30)] 

我不知道我是否真的解释过自己,但我想你会大概理解了这些例子。



我试过了,但它不起作用:

<$ p $ ((a,b):[])= [(a,b)]
test((a,b):( c,d):xs) a == c =(a,b + d):test((a,b):xs)
|否则=(c,d):test((a,b):xs)


解决方案

  import Data.List 

answer :: [(String,Int)] - > [(String,Int)]
answer = map(foo。unzip)。 groupBy(\ xy - > fst x == fst y)。 sort
where foo(names,vals)=(head names,sum vals)

一个相当直接的方法。
首先,点(。)表示函数组合,它允许我们将值从一个函数传递到下一个函数,也就是说,一个函数的输出成为输入下一个,等等。我们首先应用 sort ,它会自动将列表中的名称相邻。接下来,我们使用 groupBy 将每个具有相似名称的对放在一个列表中。我们最后列出了一个列表,每个列表包含具有相似名称的对:

[[(Bradley,30)],[( John,10),(John,45)],[(Mary,10),(Mary,15)]]

b $ b

给出这样一个列表,你将如何处理每个子列表?
也就是说,如何处理包含所有相同名称的列表?



显然,我们希望将它们缩小为一个包含名称和值的总和。为了达到这个目的,我选择了(foo。unzip)这个函数,但还有很多其他的方法可以解决这个问题。 unzip 取对子列表并创建一对。该对包含2个列表,第一个包含所有名称,第二个包含所有值。如前所述,这一对通过函数组合传递给 foo foo 使用模式将其分开,然后将头部应用于名称,只返回一个名称都一样),并将 sum 应用于值列表。 sum 是另一个标准列表函数,它自然会将列表中的值相加。



然而,<(foo.unzip)仅适用于单个列表对,但我们有一个列表列表。这是 map 出现的地方。 map 将应用我们的(foo。unzip) code>函数添加到列表中的每个列表,或者更一般地说,列表中的每个元素。我们最终得到一个包含将(foo。unzip)应用于每个子列表的结果的列表。



建议查看 Data.List 中使用的所有列表函数。

I've been trying to solve this, but I just can't figure it out. So, I've a list with tuples, for example:

[("Mary", 10), ("John", 45), ("Bradley", 30), ("Mary", 15), ("John", 10)]

and what I want to get is a list with also tuples where, if the name is the same, the numbers of those tuples should be added and, if not, that tuple must be part of the final list too, exemplifying:

[("Mary",25), ("John", 55), ("Bradley", 30)]

I don't know if I explained myself really well, but I think you'll probably understand with the examples.

I've tried this, but it doesn't work:

test ((a,b):[]) = [(a,b)]
test ((a,b):(c,d):xs) | a == c = (a,b+d):test((a,b):xs)
                      | otherwise = (c,d):test((a,b):xs)

解决方案

Here's another way using lists:

import Data.List

answer :: [(String, Int)] -> [(String, Int)]
answer = map (foo . unzip) . groupBy (\x y -> fst x == fst y) . sort            
   where foo (names, vals) = (head names, sum vals)

It's a fairly straightforward approach. First, the dot (.) represents function composition which allows us to pass values from one function to the next, that is, the output of one becomes the input of the next, and so on. We start by applying sort which will automatically move the names next to one another in the list. Next we use groupBy to put each pair with similar names into a single list. We end up with a list of lists, each containing pairs with similar names:

[[("Bradley",30)], [("John",10),("John",45)], [("Mary",10),("Mary", 15)]]

Given such a list, how would you handle each sublist? That is, how would you handle a list containing all the same names?

Obviously we wish to shrink them down into a single pair, which contains the name and the sum of the values. To accomplish this, I chose the function (foo . unzip), but there are many other ways to go about it. unzip takes a list of pairs and creates a single pair. The pair contains 2 lists, the first with all the names, the second with all the values. This pair is then passed to foo by way of function composition, as discussed earlier. foo picks it apart using a pattern, and then applies head to the names, returning only a single name (they're all the same), and applying sum to the list of values. sum is another standard list function that sums the values in a list, naturally.

However, this (foo . unzip) only applies to a single list of pairs, yet we have a list of lists. This is where map comes in. map will apply our (foo . unzip) function to each list in the list, or more generally, each element in the list. We end up with a list containing the results of applying (foo . unzip) to each sublist.

I would recommend looking at all the list functions used in Data.List.

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

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