2016-10-09 60 views
0

我有下面的代碼,我想對傳遞給它(字母)的初始數組做出響應。目前它可以處理字母< = 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']

如果字母= [ 「一」, 「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']

如果字母[ 「一」, 「b」, 「C」 ,「d」,「e」,「f」,「g」]輸出將是: ['a00 b00 c00 d00 e00 f00 g00', 'a00 b00 c00 d00 e00 f00 g10',...等

+0

這段代碼應該做什麼?爲什麼不使用'itertools.combinations'? - 也爲了長度,嵌套for循環應該工作'爲我在範圍內(len(字母)):對於範圍(i)裏:' – sytech

+0

我想創建一個列表中的每個組合(最多任何長度)和相關編號。然後這個列表通過另一個迴路傳遞迴歸模型。 – sdhaus

+0

我編輯了你的代碼來添加換行符(和括號以允許)來更容易地看到這裏的重複 – Eric

回答

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)])) 
+0

'字母[:len(字母)],p [:]'更好地拼寫'字母,p' – Eric

+0

我得到的輸出通過改變if語句得到的結果是['0','1',' 2','3','4','5','6','7','8','9','10','11','12','13','14' ,'15','16','17','18','19','20','21','22','23','24','25','26',' 27','28','29','30','31','32','33','34','35','36','37','38','39' ,'40','41','42','43','44','45','46','47','48','49','50','51',' 52','53']。我用我正在尋找的輸出更新了這個問題。 – sdhaus

+0

我也編輯了答案。順便說一句。對我來說它工作得很好。對於'k = 3'和'n = 7',我得到了'['a00 b00 c00', 'a00 b00 c10', 'a00 b00 c20', 'a00 b00 c30','還有事先不需要知道組合的數量。只要將'variable_s [x]'改爲'variable_s.append()'就可以了。 – Nf4r

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
+0

我得到的輸出是用你給出的'''','1','2','3','4','5','6','7',' 8','9','10','11','12','13','14','15','16','17','18','19','20' ,'21','22','23','24','25','26']。我用我正在尋找的輸出更新了這個問題。 – sdhaus