tuples

Scala generic (tuple) type with multiple subtypes

99封情书 提交于 2019-12-05 20:23:22
I am writing a data structure (basically a hashmap) in Scala that will take one tuple (of possibly different number of arguments each time) and do something with it. To generically implement this, I defined a type: type T <: Tuple1[_] with Tuple2[_,_] with Tuple3[_,_,_] with Tuple4[_,_,_,_] with Tuple5[_,_,_,_,_] and then the data structure val map = new HashMap[Int, T] But this is ugly, since I have to change the type every time I have to handle more arguments in a tuple. Is there to define a generic tuple type? Thanks, Y.K. The first solution is to use Product , as said by @om-nom-nom.

template function with corresponding parameters to subset of tuple types

大兔子大兔子 提交于 2019-12-05 20:16:30
问题 I would like to write function as this find : multi_set<int, string, double, myType> m; //vector of tuples m.insert(/*some data*/); m.find<1,2>("something",2.123); Or m.find<0,3>(1,instanceOfMyType); m.find<1>("somethingelse"); Where find can be parametrized corresponding to any subset of tuple parameters. My code so far: template <typename ... T> class multi_set{ typedef tuple < T... > Tuple; vector<tuple<T...>> data = vector<tuple<T...>>(); public: void insert(T... t){ data.push_back(tuple

Saving dictionary whose keys are tuples with json, python

纵然是瞬间 提交于 2019-12-05 20:11:42
问题 I am writing a little program in python and I am using a dictionary whose (like the title says) keys and values are tuples. I am trying to use json as follows import json data = {(1,2,3):(a,b,c),(2,6,3):(6,3,2)} print json.dumps(data) Problem is I keep getting TypeError: keys must be a string . How can I go about doing it? I tried looking at the python documentation but didn't see any clear solution. Thanks! 回答1: You'll need to convert your tuples to strings first: json.dumps({str(k): v for k

Swift array to array of tuples

匆匆过客 提交于 2019-12-05 20:05:27
I have the following two arrays: let xaxis = ["monday", "tuesday", "wednesday", "thursday", "friday"] let yaxis = [1, 2, 3, 4, 5] I would like to merge them into an array that looks like this: [ ("monday", 1), ("tuesday", 2), ("wednesday", 3), ("thursday", 4), ("friday", 5)] Use zip and map : let xaxis = ["monday", "tuesday", "wednesday", "thursday", "friday"] let yaxis = [1, 2, 3, 4, 5] let tuples = zip(xaxis, yaxis).map { ($0, $1) } Try this: let xaxis = ["monday", "tuesday", "wednesday", "thursday", "friday"] let yaxis = [1, 2, 3, 4, 5] var newArr = [(String, Int)]() for i in 0..<xaxis

Iterating over tuple in C++17/20 [duplicate]

南楼画角 提交于 2019-12-05 19:38:49
This question already has an answer here: How can you iterate over the elements of an std::tuple? 18 answers Does anyone know of a good, clean way to iterate over a tuple in C++17 / 20? Let's say we have a bit of code like this: class Test { public: Test( int x ) : x_(x) {}; void Go() const { std::cout << "Hi!" << x_ << "\n" ; } int x_; }; int main() { std::tuple tplb{ Test{1} , Test{2} , Test{3} }; } How could we iterate through the tuple and call the Go() method on each using the latest 17/20 features? I know you could just have a vector of the object and then it works easily. My goal with

How do you add multiple tuples(lists, whatever) to a single dictionary key without merging them?

你。 提交于 2019-12-05 18:45:49
I've been trying to figure out how to add multiple tuples that contain multiple values to to a single key in a dictionary. But with no success so far. I can add the values to a tuple or list, but I can't figure out how to add a tuple so that the key will now have 2 tuples containing values, as opposed to one tuple with all of them. For instance say the dictionary = {'Key1':(1.000,2.003,3.0029)} and I want to add (2.3232,13.5232,1325.123) so that I end up with: dictionary = {'Key1':((1.000,2.003,3.0029),(2.3232,13.5232,1325.123))} (forgot a set of brackets!) If someone knows how this can be

Converting each element of a list to tuple

余生颓废 提交于 2019-12-05 18:29:28
I to convert each element of list to tuple like following : l = ['abc','xyz','test'] convert to tuple list: newl = [('abc',),('xyz',),('test',)] Actually I have dict with keys like this so for searching purpose I need to have these. You can use a list comprehension : >>> l = ['abc','xyz','test'] >>> [(x,) for x in l] [('abc',), ('xyz',), ('test',)] >>> Or, if you are on Python 2.x, you could just use zip : >>> # Python 2.x interpreter >>> l = ['abc','xyz','test'] >>> zip(l) [('abc',), ('xyz',), ('test',)] >>> However, the previous solution will not work in Python 3.x because zip now returns a

Java Tuple2 difference between using accessor method and calling the variable directly

故事扮演 提交于 2019-12-05 18:02:44
I'm using Tuple2 in my Java code, and I was wondering if there was a difference between accessing the values via the getter or just getting the variables directly. Tuple2<String,String> tuple = new Tuple2<>("Hello", "World"); //getting values directly String direct = tuple._1; //using getter String indirect = tuple._1(); The first loads a field where the second invokes the method using getField and invokeVirtual opcodes relatively. The generated byte-code looks like 13: getfield #6 // Field scala/Tuple2._1:Ljava/lang/Object; 16: checkcast #7 // class java/lang/String 19: astore_2 20: aload_1

Return tuple with smallest y value from list of tuples

北慕城南 提交于 2019-12-05 17:43:00
问题 I am trying to return a tuple the smallest second index value (y value) from a list of tuples. If there are two tuples with the lowest y value, then select the tuple with the largest x value (i.e first index). For example, suppose I have the tuple: x = [(2, 3), (4, 3), (6, 9)] The the value returned should be (4, 3) . (2, 3) is a candidate, as x[0][1] is 3 (same as x[1][1] ), however, x[0][0] is smaller than x[1][0] . So far I have tried: start_point = min(x, key = lambda t: t[1]) However,

Python: can I modify a Tuple?

て烟熏妆下的殇ゞ 提交于 2019-12-05 17:37:04
I have a 2 D tuple (Actually I thought, it was a list.. but the error says its a tuple) But anyways.. The tuple is of form: (floatnumber_val, prod_id) now I have a dictionary which contains key-> prod_id and value prod_name now.. i want to change the prod_id in tuple to prod_name So this is waht I did #if prodName is the tuple # prodDict is the dictionary for i in range(len(prodName)): key = prodName[i][1] # get the prodid if prodDict.has_key(key): value = prodDict[key] prodName[i][1] = value umm pretty straightforward but i get an error that TypeError: 'tuple' object does not support item