How to flatten a tuple in python
[(a, b, c) for a, (b, c) in l] Tuple packing and unpacking solves the problem.
[(a, b, c) for a, (b, c) in l] Tuple packing and unpacking solves the problem.
In general, the element types of a tuple aren’t the same, so map doesn’t make sense. You can define a function to handle the special case, though: scala> def map[A, B](as: (A, A))(f: A => B) = as match { case (a1, a2) => (f(a1), f(a2)) } map: [A,B](as: (A, A))(f: (A) => B)(B, B) … Read more
Tuples originated in functional programming. In (purely) functional programming, everything is immutable by design – a certain variable only has a single definition at all times, as in mathematics. The .NET designers wisely followed the same principle when integrating the functional style into C#/.NET, despite it ultimately being a primarily imperative (hybrid?) language. Note: Though … Read more
I think this is a subtle implementation of a Monad-like thing, specifically something in the same spirit of the continuation monad. Monads are a functional programming construction used to simulate state between different steps of a computation (Remember that a functional language is stateless). What a monad does is to chain different functions, creating a … Read more
I think this is what you want: >>> data = [(‘2013-01-16’, ‘AAPL’, 1), (‘2013-01-16’, ‘GOOG’, 1.5), (‘2013-01-17’, ‘GOOG’, 2), (‘2013-01-17’, ‘MSFT’, 4), (‘2013-01-18’, ‘GOOG’, 3), (‘2013-01-18’, ‘MSFT’, 3)] >>> df = pd.DataFrame(data, columns=[‘date’, ‘ticker’, ‘value’]) >>> df date ticker value 0 2013-01-16 AAPL 1.0 1 2013-01-16 GOOG 1.5 2 2013-01-17 GOOG 2.0 3 2013-01-17 MSFT … Read more
Javascript 1.7 added destructuring assignment which allows you to do essentially what you are after. function getTuple(){ return [“Bob”, 24]; } var [a, b] = getTuple(); // a === “bob” , b === 24 are both true
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
Square brackets are lists while parentheses are tuples. A list is mutable, meaning you can change its contents: >>> x = [1,2] >>> x.append(3) >>> x [1, 2, 3] while tuples are not: >>> x = (1,2) >>> x (1, 2) >>> x.append(3) Traceback (most recent call last): File “<stdin>”, line 1, in <module> AttributeError: … Read more
Well, because it was spec’ed that way, I suppose. This, the tuple assignment, is an example of pattern matching. Pattern matching happens in three places that I recall of: var PATTERN = … // or val for (PATTERN <- …) … case PATTERN => … So all these cases work: val l = List((1,’a’), (2,’b’), … Read more
Just use a list comprehension: nested_lst_of_tuples = [tuple(l) for l in nested_lst] Demo: >>> nested_lst = [[‘tom’, ‘cat’], [‘jerry’, ‘mouse’], [‘spark’, ‘dog’]] >>> [tuple(l) for l in nested_lst] [(‘tom’, ‘cat’), (‘jerry’, ‘mouse’), (‘spark’, ‘dog’)]