tuples

Are tuples of tuples allowed?

限于喜欢 提交于 2019-12-23 07:58:05
问题 I'm currently working on a class with a lot of templates and being able to build tuples of tuples would make it a lot easier But I tried this simple code in MSVC++ 2010: #include <tuple> void main() { auto x = std::make_tuple(std::make_tuple(5, true)); } And I get a compilation error. The same problem happens if I don't use std::make_tuple but directly std::tuple 's constructor. Is it a bug of MSVC or are tuples of tuples not allowed by the standard? 回答1: More data points: If we use std::tr1:

std::tie vs std::make_tuple

为君一笑 提交于 2019-12-23 07:01:24
问题 This code compiles but I'm wondering which version should be preferred: #include <iostream> #include <tuple> using namespace std; tuple<int, int, int> return_tuple1() { int a = 33; int b = 22; int c = 31; return tie(a, b, c); } tuple<int, int, int> return_tuple2() { int a = 33; int b = 22; int c = 31; return make_tuple(a, b, c); } int main() { auto a = return_tuple1(); auto b = return_tuple2(); return 0; } since the function is returning a tuple by value there shouldn't be any problem in

creating sets of tuples in python

北慕城南 提交于 2019-12-23 06:57:30
问题 How do i create a set of tuples with each tuple containing two elements? Each tuple will have an x and y value: (x,y) I have the numbers 1 through 50, and want to assign x to all values 1 through 50 and y also 1 through 50. S = {(1,1),(1,2),(1,3),(1,4)...(1,50),(2,1)......(50,50)} I tried positive = set(tuple(x,y) for x in range(1,51) for y in range(1,51)) but the error message says that a tuple only takes in one parameter. What do I need to do to set up a list of tuples? 回答1: mySet = set

adding the same variable to a list of tuples

╄→尐↘猪︶ㄣ 提交于 2019-12-23 03:57:07
问题 I've the below 3 lists and a value (country[i]) and i want to add the same country[i] to all tuples available: name = ["a", "b", "c"] age = [1, 2, 3] city = ["aaa", "bbb", "ccc"] country[i] where country[i] equals to "United States", And i used the following code: user_info = [tuple((t,)) for t in zip(name, age, city, country[i])] When executed i got the following result: [(('a', 1, 'aaa', 'U'),), (('b', 2, 'bbb', 'n'),), (('c', 3, 'ccc', 'i'),)] While what i want is: [('a', 1, 'aaa', 'United

how to produce a nested list from two lists in python

独自空忆成欢 提交于 2019-12-23 03:54:07
问题 I am a newbie in python, I have two lists: l1 = ['a','b','c','d'] l2 = ['new'] i want to get new list like this l3 = [('a','new'),('b','new'),('c','new'),('d','new')] What is the best way to combine the two lists? 回答1: >>> from itertools import product >>> l1 = ['a','b','c','d'] >>> l2 = ['new'] >>> list(product(l1,l2)) [('a', 'new'), ('b', 'new'), ('c', 'new'), ('d', 'new')] 回答2: If l2 always just has the one element there is no need to overcomplicate things l3 = [(x, l2[0]) for x in l1] 回答3

Python sum values in tuple in a list in a dictionary?

五迷三道 提交于 2019-12-23 02:56:33
问题 In my dictionary, each entry has a list of tuples (my python grammar may be wrong, please bear with me). It looks something like this: {1: [(2, 2), (4, 3), (6, 1), (7, 1), (8, 3)], 2: [(4, 1), (5, 3), (1, 2)],...} I'd like to sum the second value in the tuple for each entry, i.e.: {1: (10), 2: (5)...} I have been using different forms of result = sum(y for v in dict.itervalues() for (x, y) in v) But it adds up all of the values for both entries. 回答1: You can do something like this: Edit:

Boost Spirit char parser

血红的双手。 提交于 2019-12-23 02:44:43
问题 Here is a code sample: // file main.cpp #include <iostream> #include <boost/tuple/tuple.hpp> #include <boost/tuple/tuple_io.hpp> #include <boost/spirit/include/qi.hpp> int main() { std::string s( "1 A" ); boost::tuple<double, char> p; complex_matrix_parser::iterator b = s.begin(); complex_matrix_parser::iterator e = s.end(); qi::phrase_parse( b, e, ( qi::double_ >> qi::char_('A') ), qi::space, qi::skip_flag::postskip, p ); std::cerr << "==== " << p << std::endl; return 0; } This should print

Expression wrapping in Angular/Typescript: Need explanation of Rule of thumb for when/where it's required

℡╲_俬逩灬. 提交于 2019-12-23 01:37:20
问题 In this code, can someone explain the rule of thumb for, why/when there is a need for, what I believe is called, expression wrapping within Typescript? i.e. the '(' ')' in <[Parent, (Children[])]> . If I defined a tuple type for example and used that in the resolve implements/method signature of the main code, would you still need the '(' ')' wrapped around the array of Children? Are there other scenarios in Typescript/Angular where ' expression wrapping ' occurs too? Is the specific to

CFG for python-style tuples

蹲街弑〆低调 提交于 2019-12-23 00:24:47
问题 After having read for the zillionth time a question about "How do I parse HTML with Regex" on Stackoverflow, I got myself interested again in grammars, grabbed my university scripts and after a few minutes I wondered how I've ever passed my exams. As a simple (well, "simple" I expected it to be) exercise I tried to write a CFG that produces valid python tuples (for simplicity's sake only using the identifiers a , b and c ). After some good time I now came up with this: G = ( {Tuple, TupleItem

How can I use NHibernate with immutable type like System.Tuple?

时间秒杀一切 提交于 2019-12-22 21:16:29
问题 I have a composite mapping using System.Tuple<int,string> that looks as follows: <composite-element class="System.Tuple`2[[System.Int32, mscorlib],[System.String, mscorlib]], mscorlib"> <property name="Item1" column="DBColumn1"/> <property name="Item2" column="DBColumn2"/> </composite-element> I try messing around with BytecodeProvider , IObjectsFactory , ReflectionOptimizer and whatnot, but I can't get NHibernate to load my Tuple properly (whatever I do, NHibernate insists on creating the