2013-02-13 70 views
2

我有一個問題,我相信原因是由於插入函數的性質。 文檔說插入的工作原理是這樣:在這裏Python列表插入函數和數據類型問題

list.insert(index, object) 

的關鍵點是「對象」。爲了執行一個對值起作用的if語句,我將原始列表轉換爲一個數組。該數據類型此數組的已自動通過蟒計算爲如下:

In [25]: MinorTrendTypeArr.dtype 
Out[25]: dtype('int64') 

然而在列表

MinorTrendType 

的新的數組插入所需的項目之後,

MinorTrendTypeArr2 

如前所述,這必須是因爲插入函數的方法將對象項添加到列表中。 具有數據類型

In [25]: MinorTrendTypeArr2.dtype 
Out[25]: dtype('object') 

這是通過嘗試將所述

MinorTrendType 

變量轉換到新的陣列時,接收以下錯誤進一步證實。

In [27]: run triptrade.py 
This is the length of MinorTrendType the first time = 12356 
This is the length of MinorTrendType after adding plustwos elements = 13179 
--------------------------------------------------------------------------- 
ValueError        Traceback (most recent call last) 
/usr/lib/python2.7/dist-packages/IPython/utils/py3compat.pyc in execfile(fname, *where) 
    173    else: 
    174     filename = fname 
--> 175    __builtin__.execfile(filename, *where) 

/home/kane/tripkane_python/triptrade.py in <module>() 
    579 
    580 MinorTrendArr2 = np.array(MinorTrend, dtype = np.int64) 
--> 581 MinorTrendTypeArr2 = np.array(MinorTrendType, dtype = np.int64) 
    582 
    583 

ValueError: setting an array element with a sequence. 

但是同樣的問題在沒有輸入外部值的變量中不會出現。我認爲可能有更好的方式來做事,但我現在使用的知識非常有限。任何幫助,替代方案的建議將是最受歡迎的。注意:由於輸入項目會改變列表的長度,所以我無法在一個循環中輸入所有必需的值,除非上述提到的解決方法更簡單。

由於提前,

凱恩

for n in range(0, len(MinorTrendType)): 

    if MinorTrendTypeArr[n] == 2: 
     plustwos.append(n) 

pl = [] 
mi = [] 

PS1 = len(MinorTrendType) 
print "This is the length of MinorTrendType the first time = %d" %PS1 

for n in range(0, len(plustwos)): 
    bbgun = plustwos[n] + len(plustwos[0:n]) 
    pl.append(bbgun) 
    MinorTrendType.insert(pl[n], 22) 
    MinorTrend.insert(pl[n], MinorTrend[pl[n]]) 


PS2 = len(MinorTrendType) 
print "This is the length of MinorTrendType after adding plustwos elements = %d" %PS2 

MinorTrendArr2 = np.array(MinorTrend, dtype = np.int64) 
MinorTrendTypeArr2 = np.array(MinorTrendType, dtype = np.int64) 

UPDATE /編輯:

MinorTrendType 

是像這樣整數列表: MinorTrendType = [1,0,-1, 1,0,-1,2,-1,1,-1,0,0,0,2,1,0,0,-1,2,1,0,-2 ........ ....]

MinorTrend 

是一個索引列表,其中存在 MinorTrendType (這些值僅僅滿足某些條件)。所有我想要實現的是內 MinorTrendType 插入在2每次出現時標記和-2值和內 在correpsonding網站複製的指數值MinorTrend 我一直在使用 圖(I,X) INT試圖(MinorTrendType [ n]) 內循環和列表和列表解析沒有成功。我希望這使事情更清晰。我不在乎它是如何完成的,只是想實現它:)

+0

我有以下這個頗有幾分難度。幾個基本問​​題。 1.「爲了執行一個能夠處理值的if語句,我將原始列表轉換爲一個數組。」 - 這是爲了讓if語句更有效率嗎? Python的If語句很好地處理任何類型的數據(包括數值)。如果這就是你需要的一切,你不需要numpy。 2.爲什麼你關心數據的數據類型? Python的目的是鴨子型(即,你不關心類型,只是功能)。 – Namey 2013-02-14 02:01:38

+0

另外,我應該注意到,在Python中,所有的意圖和目的都是「對象」。任何接受'對象'的函數都可以使用任何數據類型。 – Namey 2013-02-14 02:03:22

+0

@Namey感謝您的查詢:1)我的意思是,當我將列表轉換爲數組,以便if語句識別數組中的任何值是數字(在本例中爲整數)時。當我用列表進行if語句時,它不起作用。 2)我不會給老鼠一些數據類型,我只是想讓第二條語句正常工作。我會在原始問題的編輯/更新中提供更多細節。希望這可以讓事情更好一點。如果它沒有更新,乾杯 – tripkane 2013-02-14 03:14:03

回答

0

哈利路亞!解決辦法就在於事實 MinorTrendArr2。我懷疑dtype ='object' 而不是 'int64' 。該解決方案主要表現在以下循環:

for n in range(0, len(plustwos)): 
bbgun = plustwos[n] + len(plustwos[0:n]) 
pl.append(bbgun) 
MinorTrendType.insert(pl[n], 22) 
MinorTrend.insert(pl[n], MinorTrend[pl[n]]) 

這應該閱讀:

for n in range(0, len(plustwos)): 
bbgun = plustwos[n] + len(plustwos[0:n]) 
pl.append(bbgun) 
MinorTrendType.insert(pl[n], [22]) 
MinorTrend.insert(pl[n], MinorTrend[pl[n]]) 

是該對象被插入到列表中唯一的差別就是[]周圍。那麼Python假設數組 MinorTrendArr2 是 「的Int64」 而不是對象,並允許下一個if語句的正確計算:)

乾杯