tuples

How do I reverse the order of element types in a tuple type?

允我心安 提交于 2019-11-30 00:18:36
How do I reverse the types in a tuple? For example, I want reverse_tuple<std::tuple<int, char, bool>>::type to be std::tuple<bool, char, int> . I tried doing the following but it didn't work. What did I do wrong? #include <type_traits> #include <tuple> template <typename... Ts> struct tuple_reverse; template <typename T, typename... Ts> struct tuple_reverse<std::tuple<T, Ts...>> { using type = typename tuple_reverse< std::tuple< typename tuple_reverse<std::tuple<Ts..., T>>::type > >::type; }; template <typename T> struct tuple_reverse<std::tuple<T>> { using type = std::tuple<T>; }; int main()

Returning a tuple from a function using uniform initialization syntax

强颜欢笑 提交于 2019-11-30 00:09:59
The following code compiles with clang (libc++) and fails with gcc (libstdc++). Why does gcc (libstdc++) complains about an initializer list? I thought the return argument was using uniform initialization syntax. std::tuple<double,double> dummy() { return {2.0, 3.0}; } int main() { std::tuple<double,double> a = dummy(); return 0; } Error: line 22: converting to ‘std::tuple’ from initializer \ list would use explicit constructor ‘constexpr std::tuple<_T1, _T2>::tuple(_U1&\ &, _U2&&) [with _U1 = double; _U2 = double; = void; _T\ 1 = double; _T2 = double]’ Note: GCC (libstdc++) (and clang (libc++

Javascript “tuple” notation: what is its point?

我是研究僧i 提交于 2019-11-30 00:02:29
At wtfjs , I found that the following is legal javascript. ",,," == Array((null,'cool',false,NaN,4)); // true The argument (null,'cool',false,NaN,4) looks like a tuple to me, but javascript does not have tuples! Some quick tests in my javascript console yields the following. var t = (null,'cool',false,NaN,4); // t = 4 (null,'cool',false,NaN,4) === 4; // true (alert('hello'), 42); // shows the alert and returns 42 It appears to behave exactly like a semicolon ; separated list of statements, simply returning the value of the last statement. Is there a reference somewhere that describes this

Why use tuples instead of objects?

…衆ロ難τιáo~ 提交于 2019-11-29 22:50:16
The codebase where I work has an object called Pair where A and B are the types of the first and second values in the Pair. I find this object to be offensive, because it gets used instead of an object with clearly named members. So I find this: List<Pair<Integer, Integer>> productIds = blah(); // snip many lines and method calls void doSomething(Pair<Integer, Integer> id) { Integer productId = id.first(); Integer quantity = id.second(); } Instead of class ProductsOrdered { int productId; int quantityOrdered; // accessor methods, etc } List<ProductsOrderded> productsOrdered = blah(); Many

python: create list of tuples from lists [duplicate]

一个人想着一个人 提交于 2019-11-29 22:14:49
I have two lists: x = ['1', '2', '3'] y = ['a', 'b', 'c'] and I need to create a list of tuples from these lists, as follows: z = [('1','a'), ('2','b'), ('3','c')] I tried doing it like this: z = [ (a,b) for a in x for b in y ] but resulted in: [('1', '1'), ('1', '2'), ('1', '3'), ('2', '1'), ('2', '2'), ('2', '3'), ('3', '1'), ('3', '2'), ('3', '3')] i.e. a list of tuples of every element in x with every element in y... what is the right approach to do what I wanted to do? thank you... EDIT: The other two duplicates mentioned before the edit is my fault, indented it in another for-loop by

Select value from list of tuples where condition

余生长醉 提交于 2019-11-29 21:22:03
I have a list of tuples. Every tuple has 5 elements (corresponding to 5 database columns) and I'd like to make a query select attribute1 from mylist where attribute2 = something e.g. personAge = select age from mylist where person_id = 10 Is it possible to query the list of tuples in some way? thank you If you have named tuples you can do this: results = [t.age for t in mylist if t.person_id == 10] Otherwise use indexes: results = [t[1] for t in mylist if t[0] == 10] Or use tuple unpacking as per Nate's answer. Note that you don't have to give a meaningful name to every item you unpack. You

Tuple pairs, finding minimum using python

不羁的心 提交于 2019-11-29 20:34:46
I want to find the minimum of a list of tuples sorting by a given column. I have some data arranged as a list of 2-tuples for example. data = [ (1, 7.57), (2, 2.1), (3, 1.2), (4, 2.1), (5, 0.01), (6, 0.5), (7, 0.2), (8, 0.6)] How may I find the min of the dataset by the comparison of the second number in the tuples only? i.e. data[0][1] = 7.57 data[1][1] = 2.1 min( data ) = (5, 0.01) min( data ) returns (1, 7.57) , which I accept is correct for the minimum of index 0, but I want minimum of index 1. In [2]: min(data, key = lambda t: t[1]) Out[2]: (5, 0.01) or: In [3]: import operator In [4]:

What's the meaning of “(1,) == 1,” in Python?

牧云@^-^@ 提交于 2019-11-29 20:24:35
I'm testing the tuple structure, and I found it's strange when I use the == operator like: >>> (1,) == 1, Out: (False,) When I assign these two expressions to a variable, the result is true: >>> a = (1,) >>> b = 1, >>> a==b Out: True This questions is different from Python tuple trailing comma syntax rule in my view. I ask the group of expressions between == operator. wim Other answers have already shown you that the behaviour is due to operator precedence, as documented here . I'm going to show you how to find the answer yourself next time you have a question similar to this. You can

Find the maximum value in a list of tuples in Python [duplicate]

狂风中的少年 提交于 2019-11-29 19:47:52
Possible Duplicate: Sorting or Finding Max Value by the second element in a nested list. Python I have a list with ~10^6 tuples in it like this: [(101, 153), (255, 827), (361, 961), ...] ^ ^ X Y I want to find the maximum value of the Ys in this list, but also want to know the X that it is bound to. How do I do this? Use max() : Using itemgetter() : In [53]: lis=[(101, 153), (255, 827), (361, 961)] In [81]: from operator import itemgetter In [82]: max(lis,key=itemgetter(1))[0] #faster solution Out[82]: 361 using lambda : In [54]: max(lis,key=lambda item:item[1]) Out[54]: (361, 961) In [55]:

Why in Python does 0, 0 == (0, 0) equal (0, False)

雨燕双飞 提交于 2019-11-29 19:29:09
In Python (I checked only with Python 3.6 but I believe it should hold for many of the previous versions as well): (0, 0) == 0, 0 # results in a two element tuple: (False, 0) 0, 0 == (0, 0) # results in a two element tuple: (0, False) (0, 0) == (0, 0) # results in a boolean True But: a = 0, 0 b = (0, 0) a == b # results in a boolean True Why does the result differ between the two approaches? Does the equality operator handle tuples differently? The first two expressions both parse as tuples: (0, 0) == 0 (which is False ), followed by 0 0 , followed by 0 == (0, 0) (which is still False that way