简体   繁体   English

如何处理元组列表?

[英]How do I operate on a list of lists of tuples?

I have something that looks like this: 我有这样的东西:

[[('A',2), ('C',0), ('G',0), ('T',0)]
,[('A',0), ('C',2), ('G',0), ('T',0)]
,[('A',0), ('C',0), ('G',2), ('T',0)]
,[('A',0), ('C',0), ('G',0), ('T',2)]]

I want to do operations on the second element of every tuple. 我想对每个元组的第二个元素进行操作。 I want to divide them by a certain number, so that it becomes a double. 我想将它们除以一定的数字,以使其成为两倍。

I've tried using maps, so I can print just the second element of the tuples with 我已经尝试过使用地图,所以我可以只打印元组的第二个元素

map (map snd) f

where f is the list above. 其中f是上面的列表。 I get the following: 我得到以下内容:

[[2,0,0,0],[0,2,0,0],[0,0,2,0],[0,0,0,2]]

I'm really stuck now; 我现在真的很困; I can't seem to figure out what to do next and how to do operations on those. 我似乎无法弄清楚下一步该怎么做,以及如何对它们进行操作。

You just need to use two maps, one map for traversing the outer list, and the other map for traversing the inner list. 您只需要使用两个映射,一个映射用于遍历外部列表,另一个映射用于遍历内部列表。 Some demo in ghci to make it clear: ghci中进行一些演示,以使其清楚:

λ> let x = [[('A',2),('C',0),('G',0),('T',0)],[('A',0),('C',2),('G',0),('T',0)]]
λ> map (map id) x
[[('A',2),('C',0),('G',0),('T',0)],[('A',0),('C',2),('G',0),('T',0)]]
λ> map (\y -> map id y) x
[[('A',2),('C',0),('G',0),('T',0)],[('A',0),('C',2),('G',0),('T',0)]]
λ> map (\y -> map (\(a,b) -> (a,b)) y) x
[[('A',2),('C',0),('G',0),('T',0)],[('A',0),('C',2),('G',0),('T',0)]]
λ> map (\y -> map (\(a,b) -> (a,b * 8)) y) x
[[('A',16),('C',0),('G',0),('T',0)],[('A',0),('C',16),('G',0),('T',0)]]

Notice that the first three examples written above are equivalent. 请注意,上面编写的前三个示例是等效的。 Notice how I expand each one of them. 请注意,我是如何扩展它们中的每一个的。 id is an identity function which will return the same output as input. id是一个身份函数,将返回与输入相同的输出。 In the last map expression, I apply an * 8 to the second value of the tuple. 在最后一个map表达式中,我将* 8应用于元组的第二个值。 Likewise, you implement your custom operation. 同样,您可以实现自定义操作。

The Control.Arrow module exposes a second function which you could use: Control.Arrow模块公开了您可以使用的second功能:

λ: let xs = [[('A',2), ('C',0), ('G',0), ('T',0)]]
λ: map (map (second (/ 3))) xs
[[('A',0.6666666666666666),('C',0.0),('G',0.0),('T',0.0)]]

Using lens : 使用镜头

λ: import Control.Lens
λ: import Control.Lens.Traversal
λ: let input = [[('A',2)],[('G',0),('T',1)]]
λ: over (mapped.mapped._2) ((/ 4).fromIntegral) input
[[('A',0.5)],[('G',0.0),('T',0.25)]]

If you want to get a list that consists of all of the second elements in your tuples, you can flatten your lists, using concat , and then map snd over that new list. 如果要获取包含元组中所有第二个元素的列表,可以使用concat展平列表,然后map snd到该新列表上。 This will give you a list composed of all of the second elements of your tuples. 这将为您提供一个由元组的所有第二个元素组成的列表。

secondElements = map snd . concat

This will give you a list of all of the second elements in your tuples, and you can then map your functions over it. 这将为您提供元组中所有第二个元素的列表,然后您可以在其上映射功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM