2016-01-13 135 views
1

我不是很習慣pyhton和想寫一個函數,它的陣列x作爲輸入,並給出一個數組背面(select)僅由的那些條目的輸入數組,滿足一定的屬性,例如在一定範圍內。應該做到這一點的功能如下:選擇數組元素定義一個新的數組

def select(x): 
    count = 0 
    select = []    #0 
    for i in range(0,len(x[0])): 
     if ((int(x[4][i])+0.5) > x[4][i] > (int(x[4][i])-0.5) ): 
     select[0][count]=x[0][i] #1 
     select[1][count]=x[1][i] #2 
     select[2][count]=x[4][i] #3 
     count = count + 1 
    return select 

但是,如果我調用該函數,我得到了以下錯誤消息:

IndexError: list index out of range 

線使其爲「#1」(和以下2條線也在製造麻煩,我認爲)。我想我必須以某種方式定義數組大小。在那種情況下,我怎麼能在python中做到這一點?正如我所看到的select=[]是不夠的。

親切的問候

+0

這個問題(我認爲你意識到)是select沒有[0]元素。你能告訴你期望輸入'x'和輸出'select'是否適合某些情況? – Brian

回答

1

select是一個空表開始。您正在嘗試將值分配給其當前不存在的元素。

也許你需要select = [[], [], []]

而且select的內部元素的元素,當你試圖給它們分配在#1不會也存在,#2,#3

也許這就是你想要的:

def select(x): 
    select = [[] for i in range(3)] #0 : [[], [], []] 
    for i in range(0,len(x[0])): 
     if ((int(x[4][i])+0.5) > x[4][i] > (int(x[4][i])-0.5) ): 
     select[0].append(x[0][i]) #1 
     select[1].append(x[1][i]) #2 
     select[2].append(x[4][i]) #3 
    return select 
+0

也許,但似乎是一個奇怪的選擇填補它。我認爲'select = range(3)'是更爲pythonic的方式。 – Brian

+0

他想在'select'中嵌入一些東西,所以需要一個字典或另一個列表,而不是範圍。 – jatinderjit

+0

完全,gotcha,呃! – Brian

1

作爲替代自己的過濾方法(以實現陣列過濾WRT的範圍),則可以使用下面的方法

myRange = [8, 11] 
myArr = [6, 7, 8, 9, 10, 11, 12] 
myArrFiltered = [x for x in myArr if myRange[0] <= x <= myRange[1]] 
print(myArrFiltered) 
# [8, 9, 10, 11] 
1

您的代碼不起作用,因爲select列表在索引0處沒有項目。

如果我要實現類似的功能,我會用filter與決定的函數哪些元素應該選擇:

ages = [5, 15, 3, 7, 18, 34, 9, 10] 

def check_age(age): 
    # we could implement all sorts of logic here 
    # if we return True, the element is selected 
    return age > 10 

children_over_ten = filter(check_age, ages) 

print(children_over_ten) 

或者乾脆使用列表中理解,如果選擇的標準很簡單:

ages = [5, 15, 3, 7, 18, 34, 9, 10] 
children_over_ten = [x for x in ages if x > 10] 
print(children_over_ten) 
1

您應該嘗試使用列表推導,它可以用於過濾數據。

例如具有交互式蟒會話,

>>> data = [i*0.25 for i in range(0, 21)] 
>>> data 
[0.0, 0.25, 0.5, 0.75, 1.0, 1.25, 1.5, 1.75, 2.0, 2.25, 2.5, 2.75, 3.0, 3.25, 3.5, 3.75, 4.0, 4.25, 4.5, 4.75, 5.0] 
>>> [x for x in data if abs(2.5 - x) <= 0.5] 
[2.0, 2.25, 2.5, 2.75, 3.0] 

,其選擇是在範圍內的數據[2.0,3.0]。

迭代通過陣列經常可以實現,而無需使用索引,例如

for x in array: 
    print(x) 
1

需要初始化選擇相應

def select(x): 
    count = 0 
    select = [[],[],[]]    #0 
    for i in range(0,len(x[0])): 
     if ((int(x[4][i])+0.5) > x[4][i] > (int(x[4][i])-0.5) ): 
     select[0]+=[x[0][i]] #1 
     select[1]+=[x[1][i]] #2 
     select[2]+=[x[4][i]] #3 
     count = count + 1 
    return select 
+0

感謝您的時間,在此之後,我得到'TypeError:您必須先set_array for mappable',有什麼想法?說實話,我不知道'+ ='是做什麼的。 – DonkeyKong

+0

'a + = b'是'a = a + b'的簡稱。對於列表,+進行連接。 – innoSPG

+0

@DonkeyKong,我剛剛編輯了問題,我沒有將右側列入清單。我剛剛在rhs上添加了[] []。你也可以使用'append'而不是拼接。 – innoSPG

1

或者聲明選擇= [],並且這樣來做:

def select(x): 
    count = 0 
    select = [] #0 
    for i in range(0,len(x[0])): 
     if ((int(x[4][i])+0.5) > x[4][i] > (int(x[4][i])-0.5) ): 
     row = [x[r*r][i] for r in range(3)] 
     select.append(row) 
     count = count + 1 
    return select