array â í¨ì¨ì ì¸ ì«ì ë°°ì´Â¶
This module defines an object type which can compactly represent an array of basic values: characters, integers, floating-point numbers. Arrays are mutable sequence types and behave very much like lists, except that the type of objects stored in them is constrained. The type is specified at object creation time by using a type code, which is a single character. The following type codes are defined:
í ì½ë |
C í |
íì´ì¬ í |
ìµì í¬ê¸°(ë°ì´í¸) |
ë ¸í¸ |
|---|---|---|---|---|
|
signed char |
int |
1 |
|
|
unsigned char |
int |
1 |
|
|
wchar_t |
ì ëì½ë 문ì |
2 |
(1) |
|
Py_UCS4 |
ì ëì½ë 문ì |
4 |
(2) |
|
signed short |
int |
2 |
|
|
unsigned short |
int |
2 |
|
|
signed int |
int |
2 |
|
|
unsigned int |
int |
2 |
|
|
signed long |
int |
4 |
|
|
unsigned long |
int |
4 |
|
|
signed long long |
int |
8 |
|
|
unsigned long long |
int |
8 |
|
|
float |
float |
4 |
|
|
double |
float |
8 |
ë ¸í¸:
íë«í¼ì ë°ë¼ 16ë¹í¸ ëë 32ë¹í¸ ì¼ ì ììµëë¤.
ë²ì 3.9ìì ë³ê²½:
array('u')ë ì´ì íì§ëPy_UNICODEëìwchar_t를 Cíì¼ë¡ ì¬ì©í©ëë¤.Py_UNICODEë íì´ì¬ 3.3ë¶í°wchar_tì ë³ì¹ì´ë¼ì ì´ ë³ê²½ì ëìì ìí¥ì 미ì¹ì§ ììµëë¤.Deprecated since version 3.3, will be removed in version 3.16: Please migrate to
'w'typecode.Added in version 3.13.
ë 보기
The ctypes and struct modules, as well as third-party modules like numpy, use similar â but slightly different â type codes.
ê°ì ì¤ì ííì ê¸°ê³ ìí¤í
ì²ì ìí´(ì격í ë§íìë©´ C 구íì ìí´) ê²°ì ë©ëë¤. ì¤ì í¬ê¸°ë array.itemsize ì´í¸ë¦¬ë·°í¸ë¥¼ íµí´ ì¡ì¸ì¤í ì ììµëë¤.
모ëì ë¤ì í목ì ì ìí©ëë¤:
- array.typecodes¶
ì¬ì© ê°ë¥í 모ë í ì½ëê° ìë 문ìì´.
모ëì ë¤ì íì ì ìí©ëë¤:
- class array.array(typecode[, initializer])¶
íëª©ì´ typecodeì ìí´ ì íëë ì ë°°ì´, ì íì ì¸ initializer ê°ì¼ë¡ ì´ê¸°íëëë°,
bytesëbytearrayê°ì²´, ì ëì½ë 문ìì´ ëë ì ì í íì ììì ëí ì´í°ë¬ë¸ì´ì´ì¼ í©ëë¤.bytesëbytearrayê°ì²´ê° 주ì´ì§ë©´, initializerë ì ë°°ì´ìfrombytes()ë©ìëë¡ ì ë¬ë©ëë¤; ì ëì½ë 문ìì´ì´ 주ì´ì§ë©´, initializerëfromunicode()ë©ìëë¡ ì ë¬ë©ëë¤; ê·¸ë ì§ ìì¼ë©´, initializerì ì´í°ë ì´í°ê°extend()ë©ìëì ì ë¬ëì´ ë°°ì´ì ì´ê¸° í목ë¤ì ì¶ê°í©ëë¤.Array objects support the ordinary mutable sequence operations of indexing, slicing, concatenation, and multiplication. When using slice assignment, the assigned value must be an array object with the same type code; in all other cases,
TypeErroris raised. Array objects also implement the buffer interface, and may be used wherever bytes-like objects are supported.typecode,initializerì¸ìë¡ ê°ì¬ ì´ë²¤í¸(auditing event)array.__new__를 ë°ììíµëë¤.- typecode¶
ë°°ì´ì ë§ëë ë° ì¬ì©ë typecode 문ì.
- itemsize¶
ë´ë¶ ííìì íëì ë°°ì´ í목ì ê¸¸ì´ (ë°ì´í¸).
- append(x)¶
ë°°ì´ì ëì ê° xë¡ ì í목ì ì¶ê°í©ëë¤.
- buffer_info()¶
ë°°ì´ì ë´ì©ì ë´ë ë° ì¬ì©ë ë²í¼ì íì¬ ë©ëª¨ë¦¬ 주ìì ììì ìë¡ ííí 길ì´ë¥¼ ì ê³µíë íí
(address, length)를 ë°íí©ëë¤. ë°ì´í¸ ë¨ìì ë©ëª¨ë¦¬ ë²í¼ í¬ê¸°ëarray.buffer_info()[1] * array.itemsizeë¡ ê³ì°í ì ììµëë¤. ì´ê²ì í¹ì ioctl()ì°ì°ê³¼ ê°ì ë©ëª¨ë¦¬ 주ìê° íìí ì ìì¤(ê·¸ë¦¬ê³ ê·¼ë³¸ì ì¼ë¡ ìì íì§ ìì) I/O ì¸í°íì´ì¤ë¡ ìì í ë ê°í¹ ì ì©í©ëë¤. ë°íë ì«ìë ë°°ì´ì´ ì¡´ì¬íê³ ê¸¸ì´ ë³ê²½ ì°ì°ì´ ì ì©ëì§ ìë í ì í¨í©ëë¤.ì°¸ê³
Cë C++ë¡ ìì±ë ì½ë(ì´ ì 보를 í¨ì¨ì ì¼ë¡ ì¬ì©íë ì ì¼í ë°©ë²)ìì ë°°ì´ ê°ì²´ë¥¼ ì¬ì©í ë, ë°°ì´ ê°ì²´ê° ì§ìíë ë²í¼ ì¸í°íì´ì¤ë¥¼ ì¬ì©íë ê²ì´ ì¢ìµëë¤. ì´ ë©ìëë ì´ì ë²ì ê³¼ì í¸íì±ì ìí´ ì ì§ëë©° ì ì½ëììë ì¬ì©íì§ ììì¼ í©ëë¤. ë²í¼ ì¸í°íì´ì¤ë ë²í¼ íë¡í ì½ì ì¤ëª ëì´ ììµëë¤.
- byteswap()¶
ë°°ì´ì 모ë í목ì âë°ì´í¸ ì¤ìí(byteswap)â í©ëë¤. 1, 2, 4 ëë 8ë°ì´í¸ í¬ê¸°ì ê°ì ëí´ìë§ ì§ìë©ëë¤; ë¤ë¥¸ íì ê°ì´ë©´
RuntimeErrorê° ë°ìí©ëë¤. ë°ì´í¸ ììê° ë¤ë¥¸ ì»´í¨í°ìì ìì±ë íì¼ìì ë°ì´í°ë¥¼ ì½ì ë ì ì©í©ëë¤.
- count(x)¶
ë°°ì´ ë´ìì xê° ë±ì¥íë íì를 ë°íí©ëë¤.
- extend(iterable)¶
iterableì í목ì ë°°ì´ì ëì ì¶ê°í©ëë¤. iterableì´ ë¤ë¥¸ ë°°ì´ì´ë©´, ì íí ê°ì í ì½ë를 ê°ì ¸ì¼ í©ëë¤; ê·¸ë ì§ ìì¼ë©´,
TypeErrorê° ë°ìí©ëë¤. iterableì´ ë°°ì´ì´ ìëë©´, ì´í°ë¬ë¸ì´ì´ì¼ íë©° ììë ë°°ì´ì ì¶ê°í ì¬ë°ë¥¸ íì´ì´ì¼ í©ëë¤.
- frombytes(buffer)¶
ë°ì´í¸ì´ë¥ ê°ì²´ìì í목ì ì¶ê°í©ëë¤. ë´ì©ì 기ê³ê°(machine value)ì ë°°ì´ë¡ í´ìí©ëë¤ (ë§ì¹
fromfile()ë©ìë를 ì¬ì©íì¬ íì¼ìì ì½ì ê²ì²ë¼).Added in version 3.2:
fromstring()ì ëª ííê² íë ¤ê³frombytes()ë¡ ì´ë¦ì ë°ê¿¨ìµëë¤.
- fromfile(f, n)¶
íì¼ ê°ì²´ fìì (기ê³ê°ì¼ë¡) n í목ì ì½ê³ ë°°ì´ì ëì ì¶ê°í©ëë¤. n 미ë§ì íëª©ë§ ì¬ì©í ì ìì¼ë©´
EOFErrorê° ë°ìíì§ë§, ì¬ì© ê°ë¥í í목ì ì¬ì í ë°°ì´ì ì½ì ë©ëë¤.
- fromlist(list)¶
리ì¤í¸ìì í목ì ì¶ê°í©ëë¤. ì´ê²ì í ìë¬ê° ìì¼ë©´ ë°°ì´ì´ ë³ê²½ëì§ ìëë¤ë ì ë§ ì ì¸íë©´
for x in list: a.append(x)ì ëë±í©ëë¤.
- fromunicode(s)¶
주ì´ì§ ì ëì½ë 문ìì´ì ë°ì´í°ë¡ ì´ ë°°ì´ì íì¥í©ëë¤. ë°°ì´ì í ì½ëê°
'u'ë'w'ì´ì´ì¼ í©ëë¤; ê·¸ë ì§ ìì¼ë©´ValueErrorê° ë°ìí©ëë¤. ë¤ë¥¸ íì ë°°ì´ì ì ëì½ë ë°ì´í°ë¥¼ ì¶ê°íë ¤ë©´array.frombytes(unicodestring.encode(enc))를 ì¬ì©íììì¤.
- index(x[, start[, stop]])¶
Return the smallest i such that i is the index of the first occurrence of x in the array. The optional arguments start and stop can be specified to search for x within a subsection of the array. Raise
ValueErrorif x is not found.ë²ì 3.10ìì ë³ê²½: Added optional start and stop parameters.
- insert(i, x)¶
i ìì¹ ìì ê°ì´ xì¸ ì í목ì ë°°ì´ì ì½ì í©ëë¤. ìì ê°ì ë°°ì´ ëì ìëì ì¸ ê°ì¼ë¡ ì²ë¦¬ë©ëë¤.
- pop([i])¶
ë°°ì´ìì ì¸ë±ì¤ iì ìë í목ì ì ê±°íê³ ì´ë¥¼ ë°íí©ëë¤. ì íì ì¸ìì 기본ê°ì
-1ì´ë¯ë¡, 기본ì ì¼ë¡ ë§ì§ë§ íëª©ì´ ì ê±°ëê³ ë°íë©ëë¤.
- remove(x)¶
ë°°ì´ìì 첫 ë²ì§¸ x를 ì ê±°í©ëë¤.
- clear()¶
ë°°ì´ìì 모ë í목ì ì ê±°í©ëë¤.
Added in version 3.13.
- reverse()¶
ë°°ì´ì í목 ìì를 ë¤ì§ìµëë¤.
- tobytes()¶
ë°°ì´ì 기ê³ê° ë°°ì´ë¡ ë³ííê³ ë°ì´í¸ì´ íí(
tofile()ë©ìëë¡ íì¼ì 기ë¡ë ë°ì´í¸ ìíì¤ì ê°ìµëë¤)ì ë°íí©ëë¤.Added in version 3.2:
tostring()ì ëª ííê² íë ¤ê³tobytes()ë¡ ì´ë¦ì ë°ê¿¨ìµëë¤.
- tofile(f)¶
모ë í목ì (기ê³ê°ì¼ë¡) íì¼ ê°ì²´ fì ìëë¤.
- tolist()¶
ë°°ì´ì ê°ì íëª©ì´ ìë ì¼ë° 리ì¤í¸ë¡ ë³íí©ëë¤.
- tounicode()¶
ë°°ì´ì ì ëì½ë 문ìì´ë¡ ë³íí©ëë¤. ë°°ì´ì í ì½ëê°
'u'ë'w'ì´ì´ì¼ í©ëë¤; ê·¸ë ì§ ìì¼ë©´ValueErrorê° ë°ìí©ëë¤. ë¤ë¥¸ íì ë°°ì´ë¡ë¶í° ì ëì½ë 문ìì´ì ì»ì¼ë ¤ë©´array.tobytes().decode(enc)를 ì¬ì©íììì¤.
ë°°ì´ ê°ì²´ì 문ìì´ ííì array(typecode, initializer) íí를 ì·¨í©ëë¤. ë°°ì´ì´ ë¹ì´ ìì¼ë©´ initializerê° ìëµëê³ , ê·¸ë ì§ ìì¼ë©´ typecodeê° 'u' ë 'w' ì¸ ê²½ì° ì ëì½ë 문ìì´ì´ ëê³ , ê·¸ë ì§ ìì¼ë©´ ì«ì 리ì¤í¸ê° ë©ëë¤. 문ìì´ ííì eval()ì ì¬ì©íì¬ ê°ì íê³¼ ê°ì ê°ë ë°°ì´ë¡ ë¤ì ë³íë ì ììì´ ë³´ì¥ë©ëë¤. ë¨ from array import array를 ì¬ì©íì¬ array í´ëì¤ë¥¼ ìí¬í¸ íë¤ê³ ê°ì í©ëë¤. ë³ì inf ì nan ë í´ë¹ ë¶ëììì ê°ì í¬í¨íë ê²½ì° ì ìí´ì¼í©ëë¤. ì:
array('l')
array('w', 'hello \u2641')
array('l', [1, 2, 3, 4, 5])
array('d', [1.0, 2.0, 3.14, -inf, nan])