tuples:
1. an ordered sequence of elements, 元素的类型可以是任何类型 而且还可以混合元素的类型
2. immutable 不可变的 不可以改变里面的元素的值 跟strings 一样
3. creat a tuple:
t = () 空元组
t = (2,'w',3)
4. 检索 给的 是一个元素 t[1] 给的是'w' 而切片给的是一个元组 t[1:2] 给的是 一个tuple ('w',)
5. tuple 的好处
6. tuple 的级联 只能是 tuple 和tuple
e.g.
>>> ans = ()
>>> ans = ans + (1,2,3)[1]
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
ans = ans + (1,2,3)[1]
TypeError: can only concatenate tuple (not "int") to tuple
>>> ans = ans + (1,2,3)
>>> ans
(1, 2, 3)
list 列表:
list 跟 tuple的 4,5性质一样 index 得到的 都是元素的类型 而切片得到的是一个list
级联也只能 list 跟list 级联
单元素的list 表示方法 跟tuple 不一样 list: a = [1] type(a) = list
tuple : a = (1,) b=(1) a是元组 而b是int
The list data type has some more methods. Here are all of the methods of list objects:
-
Add an item to the end of the list. Equivalent to
a[len(a):] = [x]
.
list.
append
(
x
)
-
Extend the list by appending all the items from the iterable. Equivalent to
a[len(a):] = iterable
.
list.
extend
(
iterable
)
-
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, anda.insert(len(a), x)
is equivalent toa.append(x)
. -
也就是说 这个i 就是 插入x 以后 x 的位置,也就是说x要插在i位置前一个。
list.
insert
(
i,
x
)
-
Remove the first item from the list whose value is x. It is an error if there is no such item.
list.
remove
(
x
)
-
Remove the item at the given position in the list, and return it. If no index is specified,
a.pop()
removes and returns the last item in 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.)
list.
pop
(
[
i
]
)
-
Remove all items from the list. Equivalent to
del a[:]
.
list.
clear
(
)
-
Return zero-based index in the list of the first item whose value is x. Raises a
ValueError
if there is no such item.The optional arguments start and end are interpreted as in the slice notation and are used to limit the search to a particular subsequence of the list. The returned index is computed relative to the beginning of the full sequence rather than the start argument.
list.
index
(
x
[,
start
[,
end
]
]
)
-
Return the number of times x appears in the list.
list.
count
(
x
)
-
Sort the items of the list in place (the arguments can be used for sort customization, see
sorted()
for their explanation).
list.
sort
(
key=None,
reverse=False
)
-
Reverse the elements of the list in place.
list.
reverse
(
)
-
Return a shallow copy of the list. Equivalent to
a[:]
.
list.
copy
(
)
An example that uses most of the list methods:
>>> fruits = ['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana']
>>> fruits.count('apple')
2
>>> fruits.count('tangerine')
0
>>> fruits.index('banana')
3
>>> fruits.index('banana', 4) # Find next banana starting a position 4
6
>>> fruits.reverse()
>>> fruits
['banana', 'apple', 'kiwi', 'banana', 'pear', 'apple', 'orange']
>>> fruits.append('grape')
>>> fruits
['banana', 'apple', 'kiwi', 'banana', 'pear', 'apple', 'orange', 'grape']
>>> fruits.sort()
>>> fruits
['apple', 'apple', 'banana', 'banana', 'grape', 'kiwi', 'orange', 'pear']
>>> fruits.pop()
'pear'
You might have noticed that methods like insert
, remove
or sort
that only modify the list have no return value printed – they return the default None
. [1] This is a design principle for all mutable data structures in Python.
list的这些内嵌函数 返回值基本都是 None 这也是 python里 所有 可变更数据类型 的东东
a=b a,b有一个变 另一个也会变 a=b[:] 克隆 a b 是两个不同的structure 一个变不会使另一个也变
sort 和 sorted
sort 返回的是None 而且改变了warm
sorted 返回了 把cool 排序后的一个新的list 而没有改变cool
map: 由于在python 3 中 map 返回的是迭代器 所以要想 显示真正的结果 还要用到list
如下图
list(a) 之后才显示的是 一个列表