栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

给定大小的值范围内的组合

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

给定大小的值范围内的组合

A.两个参数解决方案(两列输出)

您可以使用生成这些索引,

np.indices
然后通过转置和重塑来完成工作-

np.indices((32,30)).T.reshape(-1,2)

样本输出-

In [36]: np.indices((32,30)).T.reshape(-1,2)Out[36]: array([[ 0,  0],       [ 1,  0],       [ 2,  0],       ...,        [29, 29],       [30, 29],       [31, 29]])

运行时测试-

In [74]: points = [32,30]# @218's solnIn [75]: %timeit np.transpose(np.nonzero(np.ones(points[::-1])))[:,::-1]100000 loops, best of 3: 18.6 µs per loopIn [76]: %timeit np.indices((points)).T.reshape(-1,2)100000 loops, best of 3: 16.1 µs per loopIn [77]: points = [320,300]# @218's solnIn [78]: %timeit np.transpose(np.nonzero(np.ones(points[::-1])))[:,::-1]100 loops, best of 3: 2.14 ms per loopIn [79]: %timeit np.indices((points)).T.reshape(-1,2)1000 loops, best of 3: 1.26 ms per loop

进一步提升性能

我们可以通过使用

points
with翻转,
np.indices
然后使用
np.column_stack
来创建最终的
2
column数组来进一步优化它。让我们花些时间并对照已经提出的方案进行验证。在下面列出这两种方法-

def app1(points):    return np.indices((points)).T.reshape(-1,2)def app2(points):    R,C = np.indices((points[::-1]))    return np.column_stack((C.ravel(), R.ravel()))

时间-

In [146]: points = [32,30]In [147]: np.allclose(app1(points), app2(points))Out[147]: TrueIn [148]: %timeit app1(points)100000 loops, best of 3: 14.8 µs per loopIn [149]: %timeit app2(points)100000 loops, best of 3: 17.4 µs per loopIn [150]: points = [320,300]In [151]: %timeit app1(points)1000 loops, best of 3: 1.1 ms per loopIn [152]: %timeit app2(points)1000 loops, best of 3: 822 µs per loop

因此,在较大的形状上效果更好。


B.通用解决方案(通用列输出)

我们将使其通用,以便我们可以使用给定的尽可能多的参数,如下所示:

def get_combinations(params, order='right'):    # params : tuple of input scalars that denotes sizes    # The order arg is used for the LSB position. So, with order='right', the    # rightmost column is the least significant, hence it will change the most    # when going through the rows. For order='left', the leftmost column    # would change the most.    all_indices = np.indices(params)    if order=='right':        return np.moveaxis(all_indices,0,-1).reshape(-1,len(params))    elif order=='left':        return all_indices.T.reshape(-1,len(params))    else:        raise Exception('Wrong side value!')

样本案例运行-

In [189]: get_combinations((2,3), order='left')Out[189]: array([[0, 0],       [1, 0],       [0, 1],       [1, 1],       [0, 2],       [1, 2]])In [191]: get_combinations((2,3,2), order='right')Out[191]: array([[0, 0, 0],       [0, 0, 1],       [0, 1, 0],       [0, 1, 1],       [0, 2, 0],       [0, 2, 1],       [1, 0, 0],       [1, 0, 1],       [1, 1, 0],       [1, 1, 1],       [1, 2, 0],       [1, 2, 1]])


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/625561.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号