Python

來源:Python Software Foundation

List的奇技淫巧:

>>> # Replace some items:
... a[0:2] = [1, 12]

>>> a
[1, 12, 123, 1234]

>>> # Remove some:
... a[0:2] = []

>>> a
[123, 1234]

>>> # Insert some:
... a[1:1] = [’bletch’, ’xyzzy’]

>>> a
[123, ’bletch’, ’xyzzy’, 1234]

>>> # Insert (a copy of) itself at the beginning

>>> a[:0] = a

>>> a
[123, ’bletch’, ’xyzzy’, 1234, 123, ’bletch’, ’xyzzy’, 1234]

>>> # Clear the list: replace all items with an empty list

>>> a[:] = []

>>> a
[]

參考資料