Python element-wise tuple operations like sum
import operator tuple(map(operator.add, a, b))
import operator tuple(map(operator.add, a, b))
The built-in function zip() will almost do what you want: >>> list(zip(*[(1, 2), (3, 4), (5, 6)])) [(1, 3, 5), (2, 4, 6)] The only difference is that you get tuples instead of lists. You can convert them to lists using list(map(list, zip(*[(1, 2), (3, 4), (5, 6)])))
n = 1 # N. . . [x[n] for x in elements]
You can do this by doing pd.DataFrame(col.tolist()) on that column: In [2]: df = pd.DataFrame({‘a’:[1,2], ‘b’:[(1,2), (3,4)]}) In [3]: df Out[3]: a b 0 1 (1, 2) 1 2 (3, 4) In [4]: df[‘b’].tolist() Out[4]: [(1, 2), (3, 4)] In [5]: pd.DataFrame(df[‘b’].tolist(), index=df.index) Out[5]: 0 1 0 1 2 1 3 4 In [6]: df[[‘b1’, … Read more
immutable objects can allow substantial optimization; this is presumably why strings are also immutable in Java, developed quite separately but about the same time as Python, and just about everything is immutable in truly-functional languages. in Python in particular, only immutables can be hashable (and, therefore, members of sets, or keys in dictionaries). Again, this … Read more
It is only required for single-item tuples to disambiguate defining a tuple or an expression surrounded by parentheses. (1) # the number 1 (the parentheses are wrapping the expression `1`) (1,) # a 1-tuple holding a number 1 For more than one item, it is no longer necessary since it is perfectly clear it is … Read more
Addressing questions one to three: one of the main applications for HLists is abstracting over arity. Arity is typically statically known at any given use site of an abstraction, but varies from site to site. Take this, from shapeless’s examples, def flatten[T <: Product, L <: HList](t : T) (implicit hl : HListerAux[T, L], flatten … Read more
Just call dict() on the list of tuples directly >>> my_list = [(‘a’, 1), (‘b’, 2)] >>> dict(my_list) {‘a’: 1, ‘b’: 2}
list( myBigList[i] for i in [87, 342, 217, 998, 500] ) I compared the answers with python 2.5.2: 19.7 usec: [ myBigList[i] for i in [87, 342, 217, 998, 500] ] 20.6 usec: map(myBigList.__getitem__, (87, 342, 217, 998, 500)) 22.7 usec: itemgetter(87, 342, 217, 998, 500)(myBigList) 24.6 usec: list( myBigList[i] for i in [87, 342, … Read more
In C++17 you can do this: std::apply(the_function, the_tuple); This already works in Clang++ 3.9, using std::experimental::apply. Responding to the comment saying that this won’t work if the_function is templated, the following is a work-around: #include <tuple> template <typename T, typename U> void my_func(T &&t, U &&u) {} int main(int argc, char *argv[argc]) { std::tuple<int, float> … Read more