小编ysa*_*oto的帖子

Newtype with Tuple

关于LearnYouaHaskell的这段代码:

newtype Pair b a = Pair {getPair :: (a,b)}
instance Functor (Pair c) where
  fmap f (Pair (x, y)) = Pair (f x, y)
Run Code Online (Sandbox Code Playgroud)

我知道第一行是从元组创建一个新的数据类型.然而,为什么参数的顺序切换Pair b agetPair :: (a,b)?如果我切换其中一个订单,那么由于定义中的模式匹配而出现错误fmap.

haskell newtype

3
推荐指数
1
解决办法
359
查看次数

PySpark 示例通过使用多列

我想从 PySpark 上的数据框中进行分层抽样。有一个sampleBy(col, fractions, seed=None)功能,但它似乎只使用一列作为层。有没有办法使用多个列作为层?

python python-2.7 apache-spark pyspark

3
推荐指数
1
解决办法
4902
查看次数

为什么排序(设置(A))比设置(排序(A))更快?

我有一系列数字.我想对它们进行排序并删除重复项.这个答案建议使用setsort进行那种操作.操作顺序不应改变结果,因此我测量了计算的时间.

from numpy.random import randint
from time import clock

n = 1000000

A=randint(0,n,n)

t1=clock()
B=sorted(set(A))
t2=clock()
C=set(sorted(A))
t3=clock()

print t2-t1, t3-t2

>>> 0.48011 1.339263
Run Code Online (Sandbox Code Playgroud)

sorted(set(A))大约比set(排序(A))快三倍.

是什么让一个比另一个更快?此外,还有更快的方法吗?

python sorting algorithm set time-complexity

1
推荐指数
1
解决办法
161
查看次数