.
>>> a_list = ['a']
>>> a_list = a_list + [2.0, 3] ①
>>> a_list ②
['a', 2.0, 3]
>>> a_list.append(True) ③
>>> a_list
['a', 2.0, 3, True]
>>> a_list.extend(['four', 'Ω']) ④
>>> a_list
['a', 2.0, 3, True, 'four', 'Ω']
>>> a_list.insert(0, 'Ω') ⑤
>>> a_list
['Ω', 'a', 2.0, 3, True, 'four', 'Ω']
① | + , . ; ( ). , , , . , a_list. , , , () , . |
② | , . , , . |
③ | append() . ( !) |
④ | . . . ., , . extend() , . |
⑤ | insert() . , . ; , 'Ω': a_list[0] a_list[6]. |
Python a_list.insert(0, value) unshift() Perl. , , . |
append() extend().
>>> a_list = ['a', 'b', 'c']
>>> a_list.extend(['d', 'e', 'f']) ①
>>> a_list
['a', 'b', 'c', 'd', 'e', 'f']
>>> len(a_list) ②
6
>>> a_list[-1]
'f'
>>> a_list.append(['g', 'h', 'i']) ③
>>> a_list
['a', 'b', 'c', 'd', 'e', 'f', ['g', 'h', 'i']]
>>> len(a_list) ④
7
>>> a_list[-1]
['g', 'h', 'i']
① | extend() , , a_list. |
② | , . |
③ | , append() , . append(), . |
④ | , ... . ? ( ) . , . , , , , . , , , . |
|
|
>>> a_list = ['a', 'b', 'new', 'mpilgrim', 'new']
>>> a_list.count('new') ①
2
>>> 'new' in a_list ②
True
>>> 'c' in a_list
False
>>> a_list.index('mpilgrim') ③
3
>>> a_list.index('new') ④
2
>>> a_list.index('c') ⑤
Traceback (innermost last):
File "<interactive input>", line 1, in?
ValueError: list.index(x): x not in list
:
( ):
"< >", 1, ?
ValueError: list.index(x): x
① | , count . |
② | , , , in , count(). in True False; , . |
③ | , - , index(). , , ( ), , , . |
④ | index() . 'new' : a_list[2] a_list[4], index() . |
⑤ | , , index() .[wa-robn.om] |
, ? , : index() , . , - (, -1). , , , . , , , - . , -1 . index() -1, , !