又重頭看 Python 自身提供的 Python Tutorial 。有兩點要記下來。
以變數形式建立 Tuple
>>> a=12345
>>> b="kkkkk"
>>> m=a,b
>>> m
(12345, 'kkkkk')
再看
>>> f=a,b,6535123.1
>>> f
(12345, 'kkkkk', 6535123.0999999996)
>>> h,j,k=f
>>> h
12345
>>> j
'kkkkk'
>>> k
6535123.0999999996
很好用。
Tuple 有以下的各種基本方法:
append(x)
Add an item to the end of the list; equivalent to a[len(a):] = [x].
extend(L)
Extend the list by appending all the items in the given list; equivalent to a[len(a):] = L.
insert(i, x)
Insert an item at a given position. The first argument is the index of the element before which to insert, so a.insert(0, x) inserts at the front of the list, and a.insert(len(a), x) is equivalent to a.append(x).
remove(x)
Remove the first item from the list whose value is x. It is an error if there is no such item.
pop([i])
Remove the item at the given position in the list, and return it. If no index is specified, a.pop() returns the last item in the list. The item is also removed from the list. (The square brackets around the i in the method signature denote that the parameter is optional, not that you should type square brackets at that position. You will see this notation frequently in the Python Library Reference.)
index(x)
Return the index in the list of the first item whose value is x. It is an error if there is no such item.
count(x)
Return the number of times x appears in the list.
sort()
Sort the items of the list, in place.
reverse()
Reverse the elements of the list, in place.
沒有留言:
張貼留言