python 通过索引迭代列表_python – NumPy – 迭代2D列表和打印(行,列)索引

使用NumPy和/或Pandas处理2D列表时遇到困难:

>获取所有元素的唯一组合的总和,而无需再次从同一行中选择(下面的数组应该是81种组合).

>打印组合中每个元素的行和列.

例如:

arr = [[1, 2, 4], [10, 3, 8], [16, 12, 13], [14, 4, 20]]

(1,3,12,20), Sum = 36 and (row, col) = [(0,0),(1,1),(2,1),(3,2)]

(4,10,16,20), Sum = 50 and (row, col) =[(0,2),(1,0),(2,0),(3,2)]

解决方法:

通过创建所有这样的组合和求和的方法:这是使用itertools.product和数组索引的矢量化方法 –

from itertools import product

a = np.asarray(arr) # Convert to array for ease of use and indexing

m,n = a.shape

combs = np.array(list(product(range(n), repeat=m)))

out = a[np.arange(m)[:,None],combs.T].sum(0)

样品运行 –

In [296]: arr = [[1, 2, 4], [10, 3, 8], [16, 12, 13], [14, 4, 20]]

In [297]: a = np.asarray(arr)

...: m,n = a.shape

...: combs = np.array(list(product(range(n), repeat=m)))

...: out = a[np.arange(m)[:,None],combs.T].sum(0)

...:

In [298]: out

Out[298]:

array([41, 31, 47, 37, 27, 43, 38, 28, 44, 34, 24, 40, 30, 20, 36, 31, 21,

37, 39, 29, 45, 35, 25, 41, 36, 26, 42, 42, 32, 48, 38, 28, 44, 39,

29, 45, 35, 25, 41, 31, 21, 37, 32, 22, 38, 40, 30, 46, 36, 26, 42,

37, 27, 43, 44, 34, 50, 40, 30, 46, 41, 31, 47, 37, 27, 43, 33, 23,

39, 34, 24, 40, 42, 32, 48, 38, 28, 44, 39, 29, 45])

记忆效率方法:这是一种不创造所有这些组合的方法,而是使用即时broadcasted总结,其理念深受this other post的启发 –

a = np.asarray(arr)

m,n = a.shape

out = a[0]

for i in range(1,m):

out = out[...,None] + a[i]

out.shape = out.size # Flatten

标签:python,pandas,numpy

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值