tuples

Finding index of pairwise elements

蹲街弑〆低调 提交于 2020-01-01 05:17:06
问题 Given the target ('b', 'a') and the inputs: x0 = ('b', 'a', 'z', 'z') x1 = ('b', 'a', 'z', 'z') x2 = ('z', 'z', 'a', 'a') x3 = ('z', 'b', 'a', 'a') The aim is to find the location of the continuous ('b', 'a') element and get the output: >>> find_ba(x0) 0 >>> find_ba(x1) 0 >>> find_ba(x2) None >>> find_ba(x3) 1 Using the pairwise recipe: from itertools import tee def pairwise(iterable): "s -> (s0,s1), (s1,s2), (s2, s3), ..." a, b = tee(iterable) next(b, None) return zip(a, b) I could do this

shapeless HList to TupleN where the tuple shape need not exactly match the HList shape

限于喜欢 提交于 2020-01-01 04:32:07
问题 I would like to create the equivalent of: def toTupleN[A1, ..., AN, L <: HList](l: L): TupleN[A1, ..., AN] Code using toTupleN should compile only when there is exactly one N combination of values in l that the tuple could be created from. Anything else should generate a compile-time error. Available implicit conversions should be taken into account. Note that there are no restrictions on the size of l or the ordering of values in it. Example: val l = 23 :: (1, "wibble") :: (2, "wobble") ::

From tuples to multiple columns in pandas

强颜欢笑 提交于 2020-01-01 04:31:29
问题 How do I convert this dataframe location value 0 (Richmond, Virginia, nan, USA) 100 1 (New York City, New York, nan, USA) 200 to this: city state region country value 0 Richmond Virginia nan USA 100 1 New York City New York nan USA 200 Note that the location column in the first dataframe contains tuples. I want to create four columns out of the location column. 回答1: new_col_list = ['city','state','regions','country'] for n,col in enumerate(new_col_list): df[col] = df['location'].apply(lambda

Concatenate tuples using sum()

佐手、 提交于 2020-01-01 01:17:13
问题 From this post I learned that you can concatenate tuples with: >>> tuples = (('hello',), ('these', 'are'), ('my', 'tuples!')) >>> sum(tuples, ()) ('hello', 'these', 'are', 'my', 'tuples!') Which looks pretty nice. But why does this work? And, is this optimum, or is there something from itertools that would be preferable to this construct? 回答1: the addition operator concatenates tuples in python: ('a', 'b')+('c', 'd') Out[34]: ('a', 'b', 'c', 'd') From the docstring of sum : Return the sum of

Matlab Table / Dataset type optimization

笑着哭i 提交于 2019-12-31 21:40:09
问题 I am searching some optimized datatypes for "observations-variables" table in Matlab, that can be fast and easily accessed by columns (through variables) and by rows (through observations). Here is сomparison of existing Matlab datatypes: Matrix is very fast, hovewer, it has no built-in indexing labels/enumerations for its dimensions, and you can't always remember variable name by column index. Table has very bad performance, especially when reading individual rows/columns in a for loop (I

All combinations of elements of two lists in Haskell

回眸只為那壹抹淺笑 提交于 2019-12-31 19:28:51
问题 Given two lists, [a, b] and [c, d] , I'd like to get the following result: [(a,c), (a,d), (b,c), (b,d)] How can I do this in Haskell? Is there a built-in function for this, or should I implement one myself? 回答1: [ (x,y) | x<-[a,b], y<-[c,d] ] This doesn't really require any further explanation, does it? 回答2: Applicative style all the way! λ> :m + Control.Applicative λ> (,) <$> ['a','b'] <*> ['c','d'] [('a','c'),('a','d'),('b','c'),('b','d')] (I've eschewed any String syntactic sugar above, in

Tuple.Create() vs new Tuple

一曲冷凌霜 提交于 2019-12-31 10:18:51
问题 Consider the following expressions: new Tuple<int,int>(1,2); Tuple.Create(1,2); Is there any difference between these two methods of Tuple creation? From my reading it seems to be more a convenient shorthand than anything like object creation in C++ (heap vs stack). 回答1: Well, this questions is old... but nevertheless I think I may contribute constructively. From the accepted answer: I suppose one benefit is that, since you don't have to specify the type with Tuple.Create, you can store

Delete duplicate tuples independent of order with same elements in generator Python 3.5

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-31 07:43:47
问题 I have a generator of tuples and I need to delete tuples containing same elements. I need this output for iterating. Input = ((1, 1), (1, 2), (1, 3), (3, 1), (3, 2), (3, 3)) Output= ((1, 1), (1, 2), (1, 3)) Order of output doesn't matter. I have checked this question but it is about lists: Delete duplicate tuples with same elements in nested list Python I use generators to achieve fastest results as the data is very large. 回答1: You can normalize the data by sorting it, then add it to a set to

Delete duplicate tuples independent of order with same elements in generator Python 3.5

心已入冬 提交于 2019-12-31 07:41:28
问题 I have a generator of tuples and I need to delete tuples containing same elements. I need this output for iterating. Input = ((1, 1), (1, 2), (1, 3), (3, 1), (3, 2), (3, 3)) Output= ((1, 1), (1, 2), (1, 3)) Order of output doesn't matter. I have checked this question but it is about lists: Delete duplicate tuples with same elements in nested list Python I use generators to achieve fastest results as the data is very large. 回答1: You can normalize the data by sorting it, then add it to a set to

Scala: apply Map to a list of tuples

拥有回忆 提交于 2019-12-31 07:31:13
问题 very simple question: I want to do something like this: var arr1: Array[Double] = ... var arr2: Array[Double] = ... var arr3: Array[(Double,Double)] = arr1.zip(arr2) arr3.foreach(x => {if (x._1 > treshold) {x._2 = x._2 * factor}}) I tried a lot differnt syntax versions, but I failed with all of them. How could I solve this? It can not be very difficult ... Thanks! 回答1: Multiple approaches to solve this, consider for instance the use of collect which delivers an immutable collection arr4 , as