tuples

How to sum a list of tuples

心已入冬 提交于 2019-12-30 04:06:08
问题 Given the following list of tuples... val list = List((1, 2), (1, 2), (1, 2)) ... how do I sum all the values and obtain a single tuple like this? (3, 6) 回答1: Using the foldLeft method. Please look at the scaladoc for more information. scala> val list = List((1, 2), (1, 2), (1, 2)) list: List[(Int, Int)] = List((1,2), (1,2), (1,2)) scala> list.foldLeft((0, 0)) { case ((accA, accB), (a, b)) => (accA + a, accB + b) } res0: (Int, Int) = (3,6) Using unzip . Not as efficient as the above solution.

Python: tuple indices must be integers, not str when selecting from mysql table

ε祈祈猫儿з 提交于 2019-12-30 03:44:39
问题 I have following method that I select all the ids from table and append them to a list and return that list. But when execute this code I end up getting tuple indicies must be integers... error. I have attached the error and the print out along with my method: def questionIds(con): print 'getting all the question ids' cur = con.cursor() qIds = [] getQuestionId = "SELECT question_id from questions_new" try: cur.execute(getQuestionId) for row in cur.fetchall(): print 'printing row' print row

Using Tuples in map, flatmap,… partial functions

自闭症网瘾萝莉.ら 提交于 2019-12-29 09:05:05
问题 If I do: val l = Seq(("un", ""), ("deux", "hehe"), ("trois", "lol")) l map { t => t._1 + t._2 } It's ok. If I do: val l = Seq(("un", ""), ("deux", "hehe"), ("trois", "lol")) l map { case (b, n) => b + n } It's ok too. But if I do: val l = Seq(("un", ""), ("deux", "hehe"), ("trois", "lol")) l map { (b, n) => b + n } It will not work. Why should I use "case" keyword to use named tuples? 回答1: The error message with 2.11 is more explanatory: scala> l map { (b, n) => b + n } <console>:9: error:

Using Tuples in map, flatmap,… partial functions

倖福魔咒の 提交于 2019-12-29 09:04:10
问题 If I do: val l = Seq(("un", ""), ("deux", "hehe"), ("trois", "lol")) l map { t => t._1 + t._2 } It's ok. If I do: val l = Seq(("un", ""), ("deux", "hehe"), ("trois", "lol")) l map { case (b, n) => b + n } It's ok too. But if I do: val l = Seq(("un", ""), ("deux", "hehe"), ("trois", "lol")) l map { (b, n) => b + n } It will not work. Why should I use "case" keyword to use named tuples? 回答1: The error message with 2.11 is more explanatory: scala> l map { (b, n) => b + n } <console>:9: error:

TMP: how to write template code which converts any struct into a tuple?

≡放荡痞女 提交于 2019-12-29 08:38:28
问题 Is it possible to use template meta-programming to convert any struct or class into a tuple? For instance: struct Foo { char c; int i; std::string s; }; typedef std::tuple< char, int, std::string > Foo_Tuple; It would be nice to have some template code which will generate Foo_Tuple automagically for me. ANSWER This is overkill for such a simple case, but for more elaborate cases (eg ORM or any time you need to write a lot of boiler-plate code, and a mere template or macro is inadequate for

Pass multiple arguments in form of tuple [duplicate]

不羁的心 提交于 2019-12-29 08:19:11
问题 This question already has answers here : What does ** (double star/asterisk) and * (star/asterisk) do for parameters? (20 answers) Closed 4 years ago . I'm passing a lot data around; specifically, I'm trying to pass the output of a function into a class and the output contains a tuple with three variables. I can't directly pass the output from my function (the tuple) into the class as in the input parameters. How can format the tuple so it is accepted by the class without input_tuple[0],

Transform tuple to dict

假如想象 提交于 2019-12-29 07:46:09
问题 How can I transform tuple like this: ( ('a', 1), ('b', 2) ) to dict: { 'a': 1, 'b': 2 } 回答1: Dict constructor can do this for you. dict(( ('a', 1), ('b', 2) )) 来源: https://stackoverflow.com/questions/3553949/transform-tuple-to-dict

scala coalesces multiple function call parameters into a Tuple — can this be disabled?

不羁岁月 提交于 2019-12-28 16:00:16
问题 This is a troublesome violation of type safety in my project, so I'm looking for a way to disable it. It seems that if a function takes an AnyRef (or a java.lang.Object), you can call the function with any combination of parameters, and Scala will coalesce the parameters into a Tuple object and invoke the function. In my case the function isn't expecting a Tuple, and fails at runtime. I would expect this situation to be caught at compile time. object WhyTuple { def main(args: Array[String]):

Scala: How to convert tuple elements to lists

主宰稳场 提交于 2019-12-28 11:54:14
问题 Suppose I have the following list of tuples: val tuples = listOfStrings.map(string => { val split = string.split(":") (split(0), split(1), split(2)) }) I would like to get the split(0) in a list, split(1) in another list and so on. A simple way this could be achieved is by writing: list1 = tuples.map(x => x._1).toList list2 = tuples.map(x => x._2).toList list3 = tuples.map(x => x._3).toList Is there a more elegant (functional) way of achieving the above without writing 3 separate statements?

List += Tuple vs List = List + Tuple

╄→尐↘猪︶ㄣ 提交于 2019-12-28 06:40:09
问题 Let's say I have these assignments: points = [] point = (1, 2) How come when I do this: points += point It works completely fine, and gives me points = [1, 2]. However, If I do something like: points = points + point It gives me a TypeError: can only concatenate list (not "tuple") to list. Aren't these statements the same thing, though? 回答1: The difference, is that list += is equivalent to list.extend() , which takes any iterable and extends the list, it works as a tuple is an iterable. (And