2017-07-07 81 views
0

從我爲了初始化數組瞭解創建數組,你會叫這樣的:用自己的數據結構

from array import * 
array_example = array([type of some sort],[entries into the array]) 

其中類型某種的可以是任何東西,如一個整數。我的問題是,如果我有任何方式使用我定義的數據結構(Letter)並在初始化數組時使用該類型。

這裏是我的嘗試:

x = Letter('A') 
i = type(x) 
array = array(i,[x]) 

,我然後得到了以下錯誤:

builtins.TypeError:陣列()參數1必須是Unicode字符,而不是鍵入

很抱歉,如果這是一個愚蠢的問題

class Letter: 

    def __init__(self, letter): 
     """ 
     ------------------------------------------------------- 
     Initialize a Letter object. 
     Use: l = Letter(char) 
     ------------------------------------------------------- 
     Preconditions: 
      letter - an single uppercase letter of the alphabet (str) 
     Postconditions: 
      Letter values are set. 
     ------------------------------------------------------- 
     """ 
     assert letter.isalpha() and letter.isupper(), "Invalid letter" 

     self.letter = letter 
     self.count = 0 
     self.comparisons = 0 
     return 

    def __str__(self): 
     """ 
     ------------------------------------------------------- 
     Creates a formatted string of Letter data. 
     Use: print(m) 
     Use: s = str(m) 
     ------------------------------------------------------- 
     Postconditions: 
      returns: 
      the value of self.letter (str) 
     ------------------------------------------------------- 
     """ 
     return "{}: {}, {}".format(self.letter, self.count, self.comparisons) 

    def __eq__(self, rs): 
     """ 
     ------------------------------------------------------- 
     Compares this Letter against another Letter for equality. 
     Use: l == rs 
     ------------------------------------------------------- 
     Preconditions: 
      rs - [right side] Letter to compare to (Letter) 
     Postconditions: 
      returns: 
      result - True if name and origin match, False otherwise (boolean) 
     ------------------------------------------------------- 
     """ 
     self.count += 1 
     self.comparisons += 1 
     result = self.letter == rs.letter 
     return result 

    def __lt__(self, rs): 
     """ 
     ------------------------------------------------------- 
     Determines if this Letter comes before another. 
     Use: f < rs 
     ------------------------------------------------------- 
     Preconditions: 
      rs - [right side] Letter to compare to (Letter) 
     Postconditions: 
      returns: 
      result - True if Letter precedes rs, False otherwise (boolean) 
     ------------------------------------------------------- 
     """ 
     self.comparisons += 1 
     result = self.letter < rs.letter 
     return result 

    def __le__(self, rs): 
     """ 
     ------------------------------------------------------- 
     Determines if this Letter precedes or is or equal to another. 
     Use: f <= rs 
     ------------------------------------------------------- 
     Preconditions: 
      rs - [right side] Letter to compare to (Letter) 
     Postconditions: 
      returns: 
      result - True if this Letter precedes or is equal to rs, 
       False otherwise (boolean) 
     ------------------------------------------------------- 
     """ 
     self.comparisons += 1 
     result = self.letter <= rs.letter 
     return result 

回答

0

正如文檔所示,數組只能包含基本類型 - 整數,字節等。

但是,似乎沒有任何理由不能在這裏使用列表。

0

正如您在docs中看到的,python array只能包含數值。

0

Python array s可以與一組有限的預定義類型一起使用。您不能將它們用於自定義類型。第一個參數確實是一個單獨的字符,它指定了數組將包含哪些允許的類型。請參閱here

0

正如在其他答案中指出的那樣,array僅適用於某些預定義的類型。這意味着你不能將它用於你自己定義的類型/類。在大多數情況下,沒有理由使用數組,但如果你覺得你絕對需要它,你可以使用一個numpy.array

import numpy as np 

x = Letter('A') 
my_array = np.array([x]) # array is a bad name 

這當然需要安裝numpy和所有的元素將作爲一般object s存儲在數組中。