Python 排列优化
Python Permutation Optimization
我正在为我的一个项目使用 python 中的排列,与大多数现有的排列函数不同,我没有获得每一个可能的解决方案,而是想获得一个附加到特定索引的...
l = [[0, 1, 2], [0, 1, 2], [0, 1, 2]]
我基本上是通过创建一个名为“索引”的列表,然后通过列表递减地从最后一个值向上溢出值(基于附加到列表的较大列表中的任何列表的长度)来做到这一点称为“长度”)...这有效,但是,我想知道是否有任何方法可以优化此脚本,因为到目前为止我觉得它很笨重...
我正在使用 Python,我想知道是否有办法优化以下代码...
def Permutation(slots, index):
# Declare Variables
lengths = []
indices = []
for slot in range(0, len(slots)):
lengths.append(len(slots[slot]))
if slot == (len(slots) - 1):
indices.append(index)
else:
indices.append(0)
# Overflow up the list
for slot in range(len(slots)-1, 0, -1):
add_to = int(indices[slot] / lengths[slot])
indices[slot] %= lengths[slot]
indices[slot - 1] += add_to
out = []
it = 0
for index in indices:
out.append(slots[it][index])
it += 1
print(out)
以上是主要功能,以下是输入输出示例:
Permutation(l, 26)
[2, 2, 2]
Permutation(l, 27)
IndexError: list index out of range
Permutation(l, 17)
[1, 2, 2]
Permutation(l, 4)
[0, 1, 1]
我知道有现有的库,但是,我想以这种特定方式实现排列...
这不是排列,而是给定索引处的乘积。您可以将索引解释为以与每个子列表的长度相对应的不同基数表示的数字。使用 divmod 将该基数中的每个“数字”转换为相应列表中的索引,就像在每次迭代时更改分母时分解固定基数中的数字一样:
def prodAtIndex(L,i):
result = [] # product fed in reversed order
for v in reversed(L): # from least significant to most
i,p = divmod(i,len(v)) # get digit in current 'base'
result.insert(0,v[p]) # build product
return result if i==0 else None # i will end at zero if in range
输出:
l = [[0, 1, 2], [0, 1, 2], [0, 1, 2]]
print(prodAtIndex(l, 26)) # [2, 2, 2]
print(prodAtIndex(l, 27)) # None
print(prodAtIndex(l, 17)) # [1, 2, 2]
print(prodAtIndex(l, 4)) # [0, 1, 1]
我正在为我的一个项目使用 python 中的排列,与大多数现有的排列函数不同,我没有获得每一个可能的解决方案,而是想获得一个附加到特定索引的...
l = [[0, 1, 2], [0, 1, 2], [0, 1, 2]]
我基本上是通过创建一个名为“索引”的列表,然后通过列表递减地从最后一个值向上溢出值(基于附加到列表的较大列表中的任何列表的长度)来做到这一点称为“长度”)...这有效,但是,我想知道是否有任何方法可以优化此脚本,因为到目前为止我觉得它很笨重...
我正在使用 Python,我想知道是否有办法优化以下代码...
def Permutation(slots, index):
# Declare Variables
lengths = []
indices = []
for slot in range(0, len(slots)):
lengths.append(len(slots[slot]))
if slot == (len(slots) - 1):
indices.append(index)
else:
indices.append(0)
# Overflow up the list
for slot in range(len(slots)-1, 0, -1):
add_to = int(indices[slot] / lengths[slot])
indices[slot] %= lengths[slot]
indices[slot - 1] += add_to
out = []
it = 0
for index in indices:
out.append(slots[it][index])
it += 1
print(out)
以上是主要功能,以下是输入输出示例:
Permutation(l, 26)
[2, 2, 2]
Permutation(l, 27)
IndexError: list index out of range
Permutation(l, 17)
[1, 2, 2]
Permutation(l, 4)
[0, 1, 1]
我知道有现有的库,但是,我想以这种特定方式实现排列...
这不是排列,而是给定索引处的乘积。您可以将索引解释为以与每个子列表的长度相对应的不同基数表示的数字。使用 divmod 将该基数中的每个“数字”转换为相应列表中的索引,就像在每次迭代时更改分母时分解固定基数中的数字一样:
def prodAtIndex(L,i):
result = [] # product fed in reversed order
for v in reversed(L): # from least significant to most
i,p = divmod(i,len(v)) # get digit in current 'base'
result.insert(0,v[p]) # build product
return result if i==0 else None # i will end at zero if in range
输出:
l = [[0, 1, 2], [0, 1, 2], [0, 1, 2]]
print(prodAtIndex(l, 26)) # [2, 2, 2]
print(prodAtIndex(l, 27)) # None
print(prodAtIndex(l, 17)) # [1, 2, 2]
print(prodAtIndex(l, 4)) # [0, 1, 1]