tuples

Is this expected C# 4.0 Tuple equality behavior?

≯℡__Kan透↙ 提交于 2019-12-18 12:55:54
问题 I'm seeing different behavior between using .Equals and == between two of .NET 4.0's new Tuple<> instances. If I have overridden Equals on the object in the Tuple<> and call .Equals on the Tuples the override of Equals will be called. If I use == on the Tuples the override of Equals is not called. Is that by design and does it make sense? EDIT: From answers and comments I can tell I'm not being clear. I know Tuple<> is a reference type and that for reference types == will check identity

Efficiently construct Pandas DataFrame from large list of tuples/rows

荒凉一梦 提交于 2019-12-18 12:53:11
问题 I've inherited a data file saved in the Stata .dta format. I can load it in with scikits.statsmodels genfromdta() function. This puts my data into a 1-dimensional NumPy array, where each entry is a row of data, stored in a 24-tuple. In [2]: st_time = time.time(); initialload = sm.iolib.genfromdta("/home/myfile.dta"); ed_time = time.time(); print (ed_time - st_time) 666.523324013 In [3]: type(initialload) Out[3]: numpy.ndarray In [4]: initialload.shape Out[4]: (4809584,) In [5]: initialload[0]

Sort Tuples Python

自古美人都是妖i 提交于 2019-12-18 12:51:27
问题 I have a list of tuples in my Blender python code scores=[(1489,"Sean"), (2850,"Bob"), (276,"Crap Player"), (78495, "Great Player"), (8473, "Damian"), (4860, "Andy"), (0, "Stephen")] I'm trying to sort them by their score by using this sorted(scores, key=lambda score: score[0], reverse=True) but this is not working. I have no idea why. Any tips? I've considered maybe a better implementation is to create a new Score class with fields name and score EDIT: Thanks guys for the fast reply it was

Typescript: Remove entries from tuple type

强颜欢笑 提交于 2019-12-18 12:36:35
问题 not sure if this is possible, but I would like to be able to define a type that converts tuples like: [number, string, undefined, number] to [number, string, number] (ie filter out undefined ). I thought about something like this: type FilterUndefined<T extends any[]> = { [i in keyof T]: T[i] extends undefined ? /* nothing? */ : T[i]; } Sadly I am am pretty sure that there is no way to achieve this. 回答1: Got it! But it need a lot of recursive magic: type PrependTuple<A, T extends Array<any>>

Using a std::tuple as key for std::unordered_map

耗尽温柔 提交于 2019-12-18 12:26:49
问题 With the code below, I get a very confusing error in MSVC that seems to suggest the key type (an std::tuple) is being converted to an std::string. #include <iostream> #include <string> #include <tuple> #include <utility> #include <unordered_map> typedef std::tuple<std::string,int,char> key_t; struct key_hash : public std::unary_function<key_t, std::size_t> { std::size_t operator()(const key_t& k) const { return std::get<0>(k)[0] ^ std::get<1>(k) ^ std::get<2>(k); } }; struct key_equal :

How to flatten a nested tuple?

做~自己de王妃 提交于 2019-12-18 11:50:41
问题 I have a nested tuple structure like (String,(String,Double)) and I want to transform it to (String,String,Double) . I have various kinds of nested tuple, and I don't want to transform each manually. Is there any convenient way to do that? 回答1: If you use shapeless, this is exactly what you need, I think. 回答2: There is no flatten on a Tupple. But if you know the structure, you can do something like this: implicit def flatten1[A, B, C](t: ((A, B), C)): (A, B, C) = (t._1._1, t._1._2, t._2)

How to query a constexpr std::tuple at compile time?

不想你离开。 提交于 2019-12-18 11:46:15
问题 In C++0x, one can create a constexpr std::tuple, e.g. like #include <tuple> constexpr int i = 10; constexpr float f = 2.4f; constexpr double d = -10.4; constexpr std::tuple<int, float, double> tup(i, f, d); One also can query a std::tuple at runtime, e.g. via int i2 = std::get<0>(tup); But it is not possible to query it at compile time, e.g., constexpr int i2 = std::get<0>(tup); will throw a compilation error (at least with the latest g++ snapshot 2011-02-19). Is there any other way to query

pair lists to create tuples in order

帅比萌擦擦* 提交于 2019-12-18 11:44:17
问题 I'd like to combine two lists. If I have the following two lists: {a,b,c,d} and {1,2,3,4} what do I need to do so that I get {{a,1}, {b,2}, {c,3}, {d,4}} ? 回答1: Here is one way: Transpose[{{a, b, c, d}, {1, 2, 3, 4}}] 回答2: An esoteric method is Flatten , which (from the Help Section on Flatten) also allows Transpose of a 'ragged' array. Flatten[ {{a, b, c, d}, {1, 2, 3, 4, 5}}, {{2}, {1}}] Out[6]= {{a, 1}, {b, 2}, {c, 3}, {d, 4}, {5}} 回答3: One possible solution is MapThread[List,{{a,b,c,d},{1

C++17: Keep only some members when tuple unpacking

[亡魂溺海] 提交于 2019-12-18 10:59:11
问题 Let's imagine you need to call the following method: std::tuple<int, int, int> foo(); In C++17, you can call the function and unpack the tuple in a single line: auto [a, b, c] = foo(); Now, how can I proceed to store only b and c and to discard a ? Currently, I'm only aware of two options: 1 - I can use a dummy variable when auto-unpacking However, the dummy variable will be unused and it will issue a warning, so if I want to silent that warning the code will be quite unpleasant to see:

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

倾然丶 夕夏残阳落幕 提交于 2019-12-18 10:10:03
问题 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. 回答1: Other answers have already shown you that the behaviour is due to operator precedence, as documented here. I'm going to