tuples

How to efficiently create a tuple of length N with code that will compile with numba?

别说谁变了你拦得住时间么 提交于 2020-07-23 06:45:29
问题 I timed two ways to create a tuple of length N. This is very fast: def createTuple(): for _ in range(100000): tuplex = (0,) * 1000 CPU times: user 439 ms, sys: 1.01 ms, total: 440 ms Wall time: 442 ms This is very fast, but doesn't compile with Numba: Invalid use of Function(<built-in function mul>) with argument(s) of type(s): (UniTuple(Literal[int](0) x 1), int64) This is much slower: def createTuple(): for _ in range(100000): tuplex = tuple(0 for _ in range(1000)) %time createTuple() CPU

How to efficiently create a tuple of length N with code that will compile with numba?

独自空忆成欢 提交于 2020-07-23 06:43:22
问题 I timed two ways to create a tuple of length N. This is very fast: def createTuple(): for _ in range(100000): tuplex = (0,) * 1000 CPU times: user 439 ms, sys: 1.01 ms, total: 440 ms Wall time: 442 ms This is very fast, but doesn't compile with Numba: Invalid use of Function(<built-in function mul>) with argument(s) of type(s): (UniTuple(Literal[int](0) x 1), int64) This is much slower: def createTuple(): for _ in range(100000): tuplex = tuple(0 for _ in range(1000)) %time createTuple() CPU

Average tuple of tuples

蓝咒 提交于 2020-07-07 11:57:27
问题 How can I average, in a generic sense, the values in a tuple of tuples such that: (1,2,3),(3,4,5) becomes: (2,3,4) 回答1: You can use zip like so: In [1]: x = ((1, 2, 3), (4, 5, 6)) In [2]: [sum(y) / len(y) for y in zip(*x)] Out[3]: [2, 3, 4] Another method using lambda with more then 2 tuples in the tuple and resulting in float's instead of int's: >>> x = ((10, 10, 10), (40, 55, 66), (71, 82, 39), (1, 2, 3)) >>> print tuple(map(lambda y: sum(y) / float(len(y)), zip(*x))) (30.5, 37.25, 29.5)

Average tuple of tuples

与世无争的帅哥 提交于 2020-07-07 11:56:05
问题 How can I average, in a generic sense, the values in a tuple of tuples such that: (1,2,3),(3,4,5) becomes: (2,3,4) 回答1: You can use zip like so: In [1]: x = ((1, 2, 3), (4, 5, 6)) In [2]: [sum(y) / len(y) for y in zip(*x)] Out[3]: [2, 3, 4] Another method using lambda with more then 2 tuples in the tuple and resulting in float's instead of int's: >>> x = ((10, 10, 10), (40, 55, 66), (71, 82, 39), (1, 2, 3)) >>> print tuple(map(lambda y: sum(y) / float(len(y)), zip(*x))) (30.5, 37.25, 29.5)

converting/constructing vector of tuples into heap

白昼怎懂夜的黑 提交于 2020-07-07 10:33:58
问题 - Construction i constructed a vector of tuples vector<tuple<int,int,int>> x . For sake of simplicity, lets assume it is constructed it this way. vector<tuple<int,int,int>> x; for (int ii=0; ii < 10; ii++){ for (int jj=0; jj < 10; jj++){ currpeak[0] = ii + rand() % 10; currpeak[1] = jj + rand() % 10; currpeak[2] = rand() % 100; x.emplace_back(currpeak[0], currpeak[1], currpeak[2]); } } Now i want to get the n largest tuples, according to the 3rd element, and append them to another variable

converting/constructing vector of tuples into heap

别来无恙 提交于 2020-07-07 10:33:06
问题 - Construction i constructed a vector of tuples vector<tuple<int,int,int>> x . For sake of simplicity, lets assume it is constructed it this way. vector<tuple<int,int,int>> x; for (int ii=0; ii < 10; ii++){ for (int jj=0; jj < 10; jj++){ currpeak[0] = ii + rand() % 10; currpeak[1] = jj + rand() % 10; currpeak[2] = rand() % 100; x.emplace_back(currpeak[0], currpeak[1], currpeak[2]); } } Now i want to get the n largest tuples, according to the 3rd element, and append them to another variable

C# ValueTuple of any size

可紊 提交于 2020-07-06 18:43:50
问题 Is it possible to write a C# method that accepts a value tuple with any number of items of the same type and converts them into a list? Edit 2/6/2019 I accepted the Provided answer as the correct one. I wanted to also provide a solution that uses a base class that is not an interface, becuase I am trying to write a conversion operator and user defined conversions from an interface are not allowed. public static class TupleExtensions { public static IEnumerable<object> Enumerate(this ValueType

Casting to a Tuple<object,object>

十年热恋 提交于 2020-06-27 19:22:01
问题 Consider this chunk of code where I'm testing an unknown variable that could be an int, MyObj, or Tuple, and I'm doing some type checking to see what it is so that I can go on and handle the data differently depending on what it is: class MyObj { } // ... void MyMethod(object data) { if (data is int) Console.Write("Datatype = int"); else if (data is MyObj) Console.Write("Datatype = MyObj"); else if (data is Tuple<object,object>) { var myTuple = data as Tuple<object,object>; if (myTuple.Item1

Casting to a Tuple<object,object>

眉间皱痕 提交于 2020-06-27 19:21:12
问题 Consider this chunk of code where I'm testing an unknown variable that could be an int, MyObj, or Tuple, and I'm doing some type checking to see what it is so that I can go on and handle the data differently depending on what it is: class MyObj { } // ... void MyMethod(object data) { if (data is int) Console.Write("Datatype = int"); else if (data is MyObj) Console.Write("Datatype = MyObj"); else if (data is Tuple<object,object>) { var myTuple = data as Tuple<object,object>; if (myTuple.Item1

How to save the dictionary that have tuple keys

萝らか妹 提交于 2020-06-27 17:17:28
问题 I would like to save the dictionary having tuple on keys. I tried pickle, ujson, json to save the dictionary. All of them did not work. The dictionary has: dictionary = {(-30, -29, -72, -71): [[-29.99867621124457, -71.75349423197208, 220], [-29.996964568219873, -71.7521560207641, 220], [-29.99696437241995, -71.7507330056961, 220], [-29.99761665426199, -71.75016101067708, 220]]} I tried: with open('depth_echo.txt', 'w') as file: file.write(ujson.dumps(dictionary) import json a, b, c = "abc"