2016-03-26 16 views
0

在IronPython 2.7.5中,我有一個返回字符串數組的函數(一個由其他人設計的backbox)。將數組連接成一個數組在ironPython 2.7.5上的.NET 4.0

函數在循環中調用。我需要逐個連接返回的數組。最終的類型也必須是一個字符串數組。

UPDATE

我的代碼:

def Myfunction(): 
     in a For Loop: 
     data_array = Anotherfunction() 
     final_data_array += data_array 

     ThirdFunction(final_data_array) # the final data type MUST be array 

我不知道如何爲數組做串聯。

因此,我將數組轉換爲列表,然後連接它們。

最後,我需要在.NET 4.0上的IronPython 2.7.5中將最終列表(保留所有restuls)轉換爲數組(它必須是數組,因爲它將用作lib函數的輸入參數)。

我的代碼:

from array import array 
    tt = ["abc", "def"] 
    array(','.join(tt)) 

我得到錯誤:

Traceback (most recent call last): 
File "<stdin>", line 1, in <module> 
TypeError: expected character, got str 

下面的代碼:

from array import array 
tt = ["abc", "def"] 
array(tt) 

我得到錯誤:

Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    TypeError: expected str, got list 

我不能使用numpy和其他軟件包。或者,如何將數組併入一個數組?

我也試過:

array('c') 

,但它僅用於字符。我需要

<type 'Array[str]'> 

有什麼建議嗎?感謝

+0

','.join(tt)的類型是str不是數組。 – Lily

回答

0

數組是一個對象等待被實例化,你不應該使用它

見: https://docs.python.org/2/library/array.html

,如果你用數組着串連列表中,因爲不同類型的

連接一個列表或陣列很容易,只能試試:

列表:

a = [1,2] 
b = [3,'hey'] 
print a+b 
>>>[1,2,3,'hey'] 

陣列:

from array import array 
a = array('i') # i = int 
b = array('i') # read the table in the documentation for understand ('letter') 
a = 1,2 
b = 3,4 
print a+b 
>>>(1,2,3,4) 

或 'STR' 使用陣列( 'U')○陣列( 'C')

如果想如列表

a = array('c') 
a = ['lel', 'hixD'] 
print a 
>>>['lel', 'hixD'] 
print type(a) 
>>><type 'list'> 

Note The 'u' typecode corresponds to Python’s unicode character. On narrow Unicode builds this is 2-bytes, on wide builds this is 4-bytes.

+0

謝謝,但是,數組('c')僅用於字符。我需要。 – Lily

+0

我認爲可以使用array('u'),@Lily >>>'u'typecode對應於Python的unicode字符。在>>>窄Unicode構建這是2個字節,在廣泛的構建這是4個字節。 – Milor123

+0

數組('u'),我得到一個元組,>>> a = array('u') >>> b = array('u') >>> a =「abc」,「def」 >>> b =「fgt」,「typ」 >>> a + b ('abc','def','fgt','typ') >>> type(a + b) Lily

0

加入str:

ss = ', '.join(tt) # result: 'abc, def' 

初始化數組「字符」或「統一」與字符串:

s2 = array('c',ss) # array('c', 'abc, def') 

我不知道這是否是你想要的。

+0

也請參閱OP中的UPDATE。 – Lily

+0

模塊數組具有特定的類型代碼,如[@ Milor123](http://stackoverflow.com/users/4941927/milor123)[鏈接]中所示(https://docs.python.org/2/library/array的.html)。該模塊表示一個基本值的數組(或字符串),您應該考慮模塊'numpy'或列表爲您的目的。 –

相關問題