tuples

Is C# 4.0 Tuple covariant

烈酒焚心 提交于 2019-11-30 18:47:01
(I would check this out for myself, but I don't have VS2010 (yet)) Say I have 2 base interfaces: IBaseModelInterface IBaseViewInterface And 2 interfaces realizing those: ISubModelInterface : IBaseModelInterface ISubViewInterface : IBaseViewInterface If I define a Tuple<IBaseModelInterface, IBaseViewInterface> I would like to set that based on the result of a factory that returns Tuple<ISubModelInterface, ISubViewInterface> . In C# 3 I can't do this even though the sub interfaces realize the base interfaces. And I'm pretty sure C# 4 lets me do this if I was using IEnumerable<IBaseModelInterface

std::get using enum class as template argument

南笙酒味 提交于 2019-11-30 18:06:21
I'm using a std::tuple and defined a class enum to somehow "naming" each of the tuple's fields, forgetting about their actual indexes. So instead of doing this: std::tuple<A,B> tup; /* ... */ std::get<0>(tup) = bleh; // was it 0, or 1? I did this: enum class Something { MY_INDEX_NAME = 0, OTHER_INDEX_NAME }; std::tuple<A,B> tup; /* ... */ std::get<Something::MY_INDEX_NAME> = 0; // I don't mind the actual index... The problem is that, as this compiled using gcc 4.5.2, I've now installed the 4.6.1 version, and my project failed to compile. This snippet reproduces the error: #include <tuple>

Remove reference from std::tuple members

让人想犯罪 __ 提交于 2019-11-30 17:56:44
问题 I'm implementing save/restore functionality for some variables with the help of stl tuples as follows: double a = 1, b = 2; int c = 3; auto tupleRef = std::make_tuple(std::ref(a), std::ref(b), std::ref(c)); // here I'm saving current state of a, b, c std::tuple<double, double, int> saved = tupleRef; //here goes block of code, where a, b, and c get spoiled ...................... // //now I'm restoring initial state of a, b, c tupleRef = savedTuple; This code works well. But instead of explicit

How to check if a tuple contains an element in Python?

☆樱花仙子☆ 提交于 2019-11-30 17:35:48
I tried to find the available methods but couldn't find it. There is no contains . Should I use index ? I just want to know if the item exists, don't need the index of it. You use in . if element in thetuple: #whatever you want to do. if "word" in str(tuple): # You can convert the tuple to str too i has the same problem and only worked to me after the convert str() Be careful with that: return Oops. use Set: d= {...} def simha(): d = ('this_is_valid') b = 'valid' if b in d: print("Oops!!!!!") simha() 来源: https://stackoverflow.com/questions/17920147/how-to-check-if-a-tuple-contains-an-element

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

心已入冬 提交于 2019-11-30 17:25:26
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 qIds.append(str(row['question_id'])) except Exception, e: traceback.print_exc() return qIds Printing what

What is the difference between assigning to std::tie and tuple of references?

穿精又带淫゛_ 提交于 2019-11-30 17:21:22
I am a bit puzzled by the following tuple business: int testint = 1; float testfloat = .1f; std::tie( testint, testfloat ) = std::make_tuple( testint, testfloat ); std::tuple<int&, float&> test = std::make_tuple( testint, testfloat ); With std::tie it works, but assigning directly to the tuple of references doesn't compile, giving "error: conversion from ‘std::tuple<int, float>’ to non-scalar type ‘std::tuple<int&, float&>’ requested" or "no suitable user-defined conversion from std::tuple<int, float> to std::tuple<int&, float&>" Why? I double checked with the compiler if it's really the same

Create a tuple from a string and a list of strings

别来无恙 提交于 2019-11-30 17:18:09
I need to combine a string along with a list of strings into a tuple so I can use it as a dictionary key. This is going to be in an inner loop so speed is important. The list will be small (usually 1, but occasionally 2 or 3 items). What is the fastest way to do this? Before: my_string == "foo" my_list == ["bar", "baz", "qux", "etc"] After: my_tuple == ("foo", "bar", "baz", "qux", "etc") (Note: my_list must not be altered itself). I can't speak for performance, but this is definitely the simplest I can think of: my_tuple = tuple([my_string] + my_list) The straightforward way is simply my_tuple

Equivalent of std::transform for tuples

∥☆過路亽.° 提交于 2019-11-30 16:57:11
问题 I want a function that will behave like std::transform for tuples. Basically the functionality to be implemented is template<size_t From, size_t To, class Tuple, class Func> void tuple_transform(Tuple&& source, Tuple&& target, Func f) { // elements <From, To> of `target` ti become `f(si)`, where // si is the corresponding element of `source` }; I believe that to implement this I'll need a compile time integer range struct, a generalization of std::index_sequence and I've implemented it here

Is there any pythonic way to find average of specific tuple elements in array?

∥☆過路亽.° 提交于 2019-11-30 16:26:33
问题 I want to write this code as pythonic. My real array much bigger than this example. ( 5+10+20+3+2 ) / 5 print(np.mean(array,key=lambda x:x[1])) TypeError: mean() got an unexpected keyword argument 'key' array = [('a', 5) , ('b', 10), ('c', 20), ('d', 3), ('e', 2)] sum = 0 for i in range(len(array)): sum = sum + array[i][1] average = sum / len(array) print(average) import numpy as np print(np.mean(array,key=lambda x:x[1])) How can avoid this? I want to use second example. I'm using Python 3.7

Compare two lists of tuples

那年仲夏 提交于 2019-11-30 16:06:49
问题 old = [('ver','1121'),('sign','89'),('address','A45'),('type','00')] new = [('ver','1121'),('sign','89'),('type','01')] I need to compare the new list against old one based on first element of the tuples, and show the difference between whatever elements new list has, so that the output should look like: Match : ver = 1121 Match : sign = 89 Mismatch : type = 01 (old : 00) I could get all the matching tuples with below list comprehension but could not think beyond it. my_list = [(a,b) for (a,b