내가 할 수 있는 방법을 알고 싶어요, 아직 어레이입니다 초기화 (또는 목록) 을 정의할 수 있는 크기 값뿐만 채울 수 있습니다.
예를 들어 C:
int x[5]; /* declared without adding elements*/
Python 에서 어떻게 그래요?
감사합니다.
이러한 질문에 답할 수 있는 t # 39, 왜 don& 답변됨 명백하네?
a = numpy.empty(n, dtype=object)
이렇게 하면 해당 오브젝트를 저장할 수 있는 길이 n 배열입니다. # 39 can& 정보기술 (it), 또는 덧붙여집니다 크기를 조정할 수 없다. 특히, t # 39 doesn& 그들이실천하는 공간을 패딩 길이는. 이는 동등한 Java& # 39 의 파이썬
Object[] a = new Object[n];
이렇게:
>>> d = [ [ None for y in range( 2 ) ] for x in range( 2 ) ]
>>> d
[[None, None], [None, None]]
>>> d[0][0] = 1
>>> d
[[1, None], [None, None]]
이런 문제가 다른 솔루션과 이어질 것으로 보인다.
>>> d = [ [ None ] * 2 ] * 2
>>> d
[[None, None], [None, None]]
>>> d[0][0] = 1
>>> d
[[1, None], [1, None]]
>>> import numpy
>>> x = numpy.zeros((3,4))
>>> x
array([[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.]])
>>> y = numpy.zeros(5)
>>> y
array([ 0., 0., 0., 0., 0.])
2d 와 y 는 x 는 어레이이며 1 일 어레이입니다. 그 둘이 함께 초기화되었습니다 제로.
>>> n = 5 #length of list
>>> list = [None] * n #populate list, length n with n entries "None"
>>> print(list)
[None, None, None, None, None]
>>> list.append(1) #append 1 to right side of list
>>> list = list[-n:] #redefine list as the last n elements of list
>>> print(list)
[None, None, None, None, 1]
>>> list.append(1) #append 1 to right side of list
>>> list = list[-n:] #redefine list as the last n elements of list
>>> print(list)
[None, None, None, 1, 1]
>>> list.append(1) #append 1 to right side of list
>>> list = list[-n:] #redefine list as the last n elements of list
>>> print(list)
[None, None, 1, 1, 1]
정말 아무것도 또는 목록에 있는 꼴입니다.
>>> n = 5 #length of list
>>> list = [] # create list
>>> print(list)
[]
>>> list.append(1) #append 1 to right side of list
>>> list = list[-n:] #redefine list as the last n elements of list
>>> print(list)
[1]
4 일 이터레이션은 덮어쓰기/추가:
>>> list.append(1) #append 1 to right side of list
>>> list = list[-n:] #redefine list as the last n elements of list
>>> print(list)
[1,1,1,1]
5 및 그 이후의 모든:
>>> list.append(1) #append 1 to right side of list
>>> list = list[-n:] #redefine list as the last n elements of list
>>> print(list)
[1,1,1,1,1]
난 이런 내용의 글을 통해 프로그램과 그 출력입니다 샘플링합니다 데 도움을 줄 수 있습니다.
프로그램:
t = input("")
x = [None]*t
y = [[None]*t]*t
for i in range(1, t+1):
x[i-1] = i;
for j in range(1, t+1):
y[i-1][j-1] = j;
print x
print y
출력: -
2
[1, 2]
[[1, 2], [1, 2]]
이 네 관련하여 몇 가지 매우 기본적인 개념을 비워집니다 바란다 "고 선언. 그들과 같은 기타 특정 값을 가지고 그 초기화중 초기화하려면 '0'. 솔리드로 선언할 수 있습니다.
x = [0]*10
이를 통해 바랍니다.!! )
사용하여 크기를 제한하는 설명자를 시도해 볼 수 있습니다
class fixedSizeArray(object):
def __init__(self, arraySize=5):
self.arraySize = arraySize
self.array = [None] * self.arraySize
def __repr__(self):
return str(self.array)
def __get__(self, instance, owner):
return self.array
def append(self, index=None, value=None):
print "Append Operation cannot be performed on fixed size array"
return
def insert(self, index=None, value=None):
if not index and index - 1 not in xrange(self.arraySize):
print 'invalid Index or Array Size Exceeded'
return
try:
self.array[index] = value
except:
print 'This is Fixed Size Array: Please Use the available Indices'
arr = fixedSizeArray(5)
print arr
arr.append(100)
print arr
arr.insert(1, 200)
print arr
arr.insert(5, 300)
print arr
출력:
[None, None, None, None, None]
Append Operation cannot be performed on fixed size array
[None, None, None, None, None]
[None, 200, None, None, None]
This is Fixed Size Array: Please Use the available Indices
[None, 200, None, None, None]
내가 해야 할 것은 내가 한 일을 쉽게 찾을 어레이에서는 설정되었습니다 예를 들어, 크기를 위해 빈 문자열을 이 더 좋아요
코드:
import numpy as np
x= np.zeros(5,str)
print x
출력:
['' '' '' '' '']
이것은 도움됐네 바랍니다:)
내장 'bytearray' 바이트입니다 작업하는 경우 사용할 수 있습니다. 다른 유형을 살펴보면 작업하는 경우 필수 내장 '어레이입니다'.
특히 '목록' 한 것을 이해할 수 없는 '어레이입니다'.
예를 들어, 만들려는 파일 내용을 읽을 수 있는 버퍼입니다 bytearray 다음과 같이 사용할 수 있습니다 (더 가지 방법이 있습니다 그러나 비유하사 유효함):
with open(FILENAME, 'rb') as f:
data = bytearray(os.path.getsize(FILENAME))
f.readinto(data)
미리 할당된 메모리를 사용할 수 있는 '이' 이 스니핏 bytearray '의 고정 길이 파일_이름 크기 (바이트) 입니다. 이 프리올로카션 버퍼의 포로토콜 보다 효율적으로 사용할 수 있는 파일을 읽지 않고 붙여넣습니다 어레이에서는 복제본에 좋은 방법이 있습니다 하지만 변경 가능 버퍼 있어 이를 통해 데이터베이스에구성원을 오토메이티드 질문이예요 1 (i believe