.


:




:

































 

 

 

 





.

>>> 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, , !





:


: 2016-11-18; !; : 460 |


:

:

! . .
==> ...

1659 - | 1447 -


© 2015-2024 lektsii.org - -

: 0.011 .