三元组列表的第一个元素的总和 [英] Sum of the first elem of a 3-tuples list

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

问题描述



我需要创建一个函数来检索三元组列表中第一个元素的总和。



我有类似的东西: , 11),(22, 22, 22),(33, 33, 33),(44, 44, 44),(55, 55, 55),(66,66,66)]

列表中每个3元组的第一个元素的总和。 = 11 + 22 + 33 + 44 + 55

模式匹配可能是? map

解决方案使用 sum 加上列表理解

  sum [x | (x,_,_)< - 元组] 


I'm new around programming stuff :/

I need to make a function that retrieve the sum of the first element from a 3-tuple list.

I have something like:

tuples = [(11,"11","11"),(22,"22","22"),(33,"33","33"),(44,"44","44"),(55,"55","55"),(66,"66","66")]

And I need the sum of the first element of each 3-tuple from the list. = 11+22+33+44+55

Pattern matching maybe? map?

解决方案

Use sum with a list comprehension:

sum [x | (x, _, _) <- tuples]

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

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