根据我的理解,为了初始化一个数组,你可以这样调用:
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:array()参数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)
如文档所示,数组只能包含基本类型 - 整数,字节等。
但似乎没有任何理由可以在这里使用列表。
答案 1 :(得分:0)
正如您在docs中看到的,python array
只能保存数值。
答案 2 :(得分:0)
Python <script>
// Javascript function to submit new chat entered by user
function submitchat(){
if($('#chat').val()=='' || $('#chatbox').val()==' ') return false;
$.ajax({
url:'chat/chat.php',
data:{chat:$('#chatbox').val(),ajaxsend:true},
method:'post',
success:function(data){
$('#result').html(data); // Get the chat records and add it to result div
$('#chatbox').val(''); //Clear chat box after successful submition
document.getElementById('result').scrollTop=document.getElementById('result').scrollHeight; // Bring the scrollbar to bottom of the chat resultbox in case of long chatbox
}
})
return false;
};
// Function to continously check the some has submitted any new chat
setInterval(function(){
$.ajax({
url:'chat/chat.php',
data:{ajaxget:true},
method:'post',
success:function(data){
$('#result').html(data);
}
})
},1000);
// Function to chat history
$(document).ready(function(){
$('#clear').click(function(){
if(!confirm('Are you sure you want to clear chat?'))
return false;
$.ajax({
url:'chat/chat.php',
data:{username:"<?php echo $_SESSION['username'] ?>",ajaxclear:true},
method:'post',
success:function(data){
$('#result').html(data);
}
})
})
})
</script>
<script>
$(document).ready(function() {
$('.entryMsg').keydown(function(event) {
if (event.keyCode == 13) {
this.form.submit();
return false;
}
});
});
</script>`
可以与一组有限的预定义类型一起使用。您不能将它们与自定义类型一起使用。第一个参数确实是单个字符,它指定数组将包含哪些允许的类型。请参阅here。
答案 3 :(得分:0)
正如其他答案中所指出的,array
仅适用于某些预定义类型。这意味着您不能将它用于您自己定义的类型/类。大多数时候,没有理由使用数组,但如果你觉得你绝对需要它,你可以使用numpy.array
:
import numpy as np
x = Letter('A')
my_array = np.array([x]) # array is a bad name
这当然需要安装numpy
,所有元素都将存储为数组中的常规object
。