tuples

Element-wise tuple addition

南楼画角 提交于 2019-12-03 05:21:55
I have some values held in a tuple, and I am looking to add another tuple to it element-wise. So I would like functionality like this: std::tuple<int,int> a = {1,2}; std::tuple<int,int> b = {2,4}; std::tuple<int,int> c = a + b; // possible syntax 1 a += b; // possible syntax 2 a += {2,4}; // possible syntax 3 Where the output tuple would have the value {3,6} Was looking at CPP reference , but I couldn't find this functionality. It's possible that this and this question are relevant, however the answers are obfuscated by other complexities. You could also consider using std::valarray since it

Save list of ordered tuples as CSV [duplicate]

社会主义新天地 提交于 2019-12-03 04:54:07
问题 This question already has answers here : Python, transposing a list and writing to a CSV file (3 answers) Closed 6 years ago . I have a list of tuples ordered by value. They are in the form (name,count) where count is number of occurrences for each unique name. I would like to take this list and transform it into CSV where each name is column header and each value is column value of a single row. Any suggestions how to do it? Thanks. 回答1: You can do this: import csv data=[('smith, bob',2),(

Scala: how can I sort an array of tuples by their second element?

≯℡__Kan透↙ 提交于 2019-12-03 04:53:34
问题 is there a way in Scala to sort an array of tuples using and arbitrary comparison function? In particular I need to sort and array of tuples by their second element, but I wanted to know a general technique to sort arrays of tuples. Thanks! 回答1: You can use this code: scala> val v = Array(('a', 2), ('b', 1)) v: Array[(Char, Int)] = Array((a,2), (b,1)) scala> scala.util.Sorting.stableSort(v, | (e1: (Char, Int), e2: (Char, Int)) => e1._2 < e2._2) scala> v res11: Array[(Char, Int)] = Array((b,1)

Swift: Get an element from a tuple

梦想的初衷 提交于 2019-12-03 04:53:05
If I have a tuple like this var answer: (number: Int, good: Bool) How do I get one of the elements? This doesn't work: answer["number"] I am modeling this question after Swift: Get an array of element from an array of tuples , but my question was a little more basic. I did find the answer buried in the documentation so I am adding my answer below in Q&A format for faster searching in the future. According to the documentation (scroll down to Tuples), there are three ways to do it. Given var answer: (number: Int, good: Bool) = (100, true) Method 1 Put the element variable name within a tuple.

How do you write the function 'pairs' in Haskell?

孤者浪人 提交于 2019-12-03 04:50:48
The pairs function needs to do something like this: pairs [1, 2, 3, 4] -> [(1, 2), (2, 3), (3, 4)] pairs [] = [] pairs xs = zip xs (tail xs) You could go as far as import Control.Applicative (<*>) pairs = zip <*> tail but pairs xs = zip xs (tail xs) is probably clearer. Just for completeness, a more "low-level" version using explicit recursion: pairs (x:xs@(y:_)) = (x, y) : pairs xs pairs _ = [] The construct x:xs@(y:_) means "a list with a head x , and a tail xs that has at least one element y ". This is because y doubles as both the second element of the current pair and the first element of

Create a slice using a tuple

孤街浪徒 提交于 2019-12-03 04:47:30
Is there any way in python to use a tuple as the indices for a slice? The following is not valid: >>> a = range(20) >>> b = (5, 12) # my slice indices >>> a[b] # not valid >>> a[slice(b)] # not valid >>> a[b[0]:b[1]] # is an awkward syntax [5, 6, 7, 8, 9, 10, 11] >>> b1, b2 = b >>> a[b1:b2] # looks a bit cleaner [5, 6, 7, 8, 9, 10, 11] It seems like a reasonably pythonic syntax so I am surprised that I can't do it. (update) And the solution turns out to be: >>> a[slice(*b)] [5, 6, 7, 8, 9, 10, 11] You can use Python's *args syntax for this: >>> a = range(20) >>> b = (5, 12) >>> a[slice(*b)] [5

What's the best way of using a pair (triple, etc) of values as one value in C#?

牧云@^-^@ 提交于 2019-12-03 04:45:51
That is, I'd like to have a tuple of values. The use case on my mind: Dictionary<Pair<string, int>, object> or Dictionary<Triple<string, int, int>, object> Are there built-in types like Pair or Triple? Or what's the best way of implementing it? Update There are some general-purpose tuples implementations described in the answers, but for tuples used as keys in dictionaries you should additionaly verify correct calculation of the hash code. Some more info on that in another question . Update 2 I guess it is also worth reminding, that when you use some value as a key in dictionary, it should be

How to pass a tuple argument the best way?

社会主义新天地 提交于 2019-12-03 04:44:13
问题 How to pass a tuple argument the best way ? Example: def foo(...): (Int, Int) = ... def bar(a: Int, b: Int) = ... Now I would like to pass the output of foo to bar . This can be achieved with: val fooResult = foo(...) bar(fooResult._1, fooResult._2) This approach looks a bit ugly, especially when we deal with a n -tuple with n > 2 . Also we have to store the result of foo in an extra value, because otherwise foo has to be executed more than once using bar(foo._1, foo._2) . Is there a better

pop/remove items out of a python tuple

↘锁芯ラ 提交于 2019-12-03 04:39:45
I am not sure if I can make myself clear but will try. I have a tuple in python which I go through as follows (see code below). While going through it, I maintain a counter (let's call it 'n') and 'pop' items that meet a certain condition. Now of course once I pop the first item, the numbering all goes wrong, how can I do what I want to do more elegantly while removing only certain entries of a tuple on the fly? for x in tupleX: n=0 if (condition): tupleX.pop(n) n=n+1 As DSM mentions, tuple 's are immutable, but even for lists, a more elegant solution is to use filter : tupleX = filter(str

Tuple syntax in VS 2017

女生的网名这么多〃 提交于 2019-12-03 04:31:47
In VS2017 RC, when you tried to use new tuple syntax, you received the following error: CS8179 Predefined type 'System.ValueTuple`X' is not defined or imported In order to use tuple syntax, you had to manually import ValueTuple nuget package into the project. Not a big deal, as it was pre-release version and I thought it will be changed in RTM so it will be enabled by default. Unfortunately in the final release version it is still the case and you have to download nuget package for every single project to use tuple syntax. Is there a way to have tuple syntax enabled for every project by