基于数组长度扩展函数

时间:2016-10-09 08:41:14

标签: python arrays itertools

我有以下代码,我想响应传递给它的初始数组(字母)。目前它可以处理字母< = 3,但我想将它扩展为n。

在这个例子中,如果数组只包含两个条目[“a”,“b”],它将触发第二个if语句。

如何更改为有效无限if语句,允许任意大小的数组?

import itertools
import numpy as np

#variable length
letters = ["a", " b", " c"]

increment = .1
d = 3
e = 3

#Calculate total possible combinations and create array
x=0
for p in itertools.product(range(d), repeat=e):
    x = x+1

variable = np.arange(x)
variable_s = [str(x) for x in variable]


x=0
#run loop based on array length.
for p in itertools.product(range(d), repeat=e):
    if len(letters) == 1:
        variable_s[x] = letters[0]+str(format(p[0]/(increment)**(-1.0),'.2f')).replace("0.", "")
    elif len(letters) == 2:
        variable_s[x] = (
            letters[0]+str(format(p[0]/(increment)**(-1.0),'.2f')).replace("0.", "")
           +letters[1]+str(format(p[1]/(increment)**(-1.0),'.2f')).replace("0.", "")
        )
    elif len(letters) == 3:
        variable_s[x] = (
            letters[0]+str(format(p[0]/(increment)**(-1.0),'.2f')).replace("0.", "")
           +letters[1]+str(format(p[1]/(increment)**(-1.0),'.2f')).replace("0.", "")
           +letters[2]+str(format(p[2]/(increment)**(-1.0),'.2f')).replace("0.", "")
        )
    x = x+1
variable_s

上面代码的输出是: ['a00 b00 c00',  'a00 b00 c10',  'a00 b00 c20',  'a00 b10 c00',  'a00 b10 c10',  'a00 b10 c20',  'a00 b20 c00',  'a00 b20 c10',  'a00 b20 c20',  'a10 b00 c00',  'a10 b00 c10',  'a10 b00 c20',  'a10 b10 c00',  'a10 b10 c10',  'a10 b10 c20',  'a10 b20 c00',  'a10 b20 c10',  'a10 b20 c20',  'a20 b00 c00',  'a20 b00 c10',  'a20 b00 c20',  'a20 b10 c00',  'a20 b10 c10',  'a20 b10 c20',  'a20 b20 c00',  'a20 b20 c10',  'a20 b20 c20']

如果letters = [“a”,“b”]输出将是: ['a00 b00',  'a00 b00',  'a00 b00',  'a00 b10',  'a00 b10',  'a00 b10',  'a00 b20',  'a00 b20',  'a00 b20',  'a10 b00',  'a10 b00',  'a10 b00',  'a10 b10',  'a10 b10',  'a10 b10',  'a10 b20',  'a10 b20',  'a10 b20',  'a20 b00',  'a20 b00',  'a20 b00',  'a20 b10',  'a20 b10',  'a20 b10',  'a20 b20',  'a20 b20',  'a20 b20']

如果字母[“a”,“b”,“c”,“d”,“e”,“f”,“g”]输出将是: ['a00 b00 c00 d00 e00 f00 g00',  'a00 b00 c00 d00 e00 f00 g10',......等。

2 个答案:

答案 0 :(得分:1)

也许尝试这样的事情?

import itertools
import numpy as np

#variable length
letters = ["a", "b", "c", "d", "e", "f", "g"]

n = len(letters) # Max limit for each element, ie. limit of 2 from [a, b], for k = 2 is ['a00 b00', 'a00 b10', 'a10 b00', 'a10 b10']
k = 3 # Number of elements we want to pick.
variable_s = []

#run loop based on array length.
for x, p in enumerate(itertools.product(range(n), repeat=k)):
    variable_s.append(' '.join([letter + str(q).zfill(2)[::-1]
                        for letter, q in zip(letters, p)]))

答案 1 :(得分:1)

这是您可以做出的最小改动 - 将您的if声明替换为:

variable_s[x] = ''.join(
    letter+str(format(p_i/(increment)**(-1.0),'.2f')).replace("0.", "")
    for letter, p_i in zip(letters, p)
)

其他说明:

  • p[1]/(increment)**(-1.0)拼写更好p[1] * increment
  • x=0; for p in itertools.product(range(d), repeat=e): x = x+1拼写更好x = d**e