纸上得来终觉浅,绝知此事要躬行!
目录
列表
列表是Python中最具灵活性的有序集合对象类型。列表可以包含任何种类的对象。列表的特点:
- 任意对象的有序集合
- 通过偏移读取
- 可变长度、异构以及任意嵌套
- 属于可变序列的分类
- 对象引用数组
help(list):
<pre>Help on class list in module builtins:
class list(object)
| list() -> new empty list
| list(iterable) -> new list initialized from iterable's items
|
| Methods defined here:
| '''
……
| '''
| __sizeof__(...)
| L.__sizeof__() -- size of L in memory, in bytes
|
| append(...)
| L.append(object) -> None -- append object to end
|
| clear(...)
| L.clear() -> None -- remove all items from L
|
| copy(...)
| L.copy() -> list -- a shallow copy of L
|
| count(...)
| L.count(value) -> integer -- return number of occurrences of value
|
| extend(...)
| L.extend(iterable) -> None -- extend list by appending elements from the iterable
|
| index(...)
| L.index(value, [start, [stop]]) -> integer -- return first index of value.
| Raises ValueError if the value is not present.
|
| insert(...)
| L.insert(index, object) -- insert object before index
|
| pop(...)
| L.pop([index]) -> item -- remove and return item at index (default last).
| Raises IndexError if list is empty or index is out of range.
|
| remove(...)
| L.remove(value) -> None -- remove first occurrence of value.
| Raises ValueError if the value is not present.
|
| reverse(...)
| L.reverse() -- reverse *IN PLACE*
|
| sort(...)
| L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE*
|
| ----------------------------------------------------------------------
| Data and other attributes defined here:
|
| __hash__ = None
</pre>
常用列表操作包括赋值、索引、切片、合并、重复、迭代、增减、排序、搜索、插入、反转等。
列表应用实例
append和sort原处修改相关的列表对象,而结果并没有返回列表。
# 解析
In[7]:3 in [1,2,3]
Out[7]:True
In [20]:list(map(abs,[0,-1,2.3,-4]))
Out[20]:[0, 1, 2.3, 4]
# 排序
In[34]:L=["日","月","星","辰"]
L.sort()
L
Out[34]:['日', '星', '月', '辰']
字典
列表是有序对象的集合,字典可以认为是无序的集合。字典与列表的差别:字典当中的元素通过键来存取,而不是偏移。
- 通过键而不是偏移量来读取
- 任意对象的无序集合
- 可变长、异构、任意嵌套
- 属于可映射类型
- 对象引用表
help(dict):
<pre>Help on class list in module builtins:
class list(object)
| list() -> new empty list
| list(iterable) -> new list initialized from iterable's items
|
| Methods defined here:
|
……
| __sizeof__(...)
| L.__sizeof__() -- size of L in memory, in bytes
|
| append(...)
| L.append(object) -> None -- append object to end
|
| clear(...)
| L.clear() -> None -- remove all items from L
|
| copy(...)
| L.copy() -> list -- a shallow copy of L
|
| count(...)
| L.count(value) -> integer -- return number of occurrences of value
|
| extend(...)
| L.extend(iterable) -> None -- extend list by appending elements from the iterable
|
| index(...)
| L.index(value, [start, [stop]]) -> integer -- return first index of value.
| Raises ValueError if the value is not present.
|
| insert(...)
| L.insert(index, object) -- insert object before index
|
| pop(...)
| L.pop([index]) -> item -- remove and return item at index (default last).
| Raises IndexError if list is empty or index is out of range.
|
| remove(...)
| L.remove(value) -> None -- remove first occurrence of value.
| Raises ValueError if the value is not present.
|
| reverse(...)
| L.reverse() -- reverse *IN PLACE*
|
| sort(...)
| L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE*
|
| ----------------------------------------------------------------------
| Data and other attributes defined here:
|
| __hash__ = None
</pre>
字典应用实例
每当对新字典键进行赋值(之前不存在的键),就会在字典内生成一个新的元素。这与列表是不同的,Python会将超过列表末尾的偏移视为越界并报错,扩充列表需要append方法或分片赋值来实现。
字典的update方法类似于合并,但是它与顺序无关,它将一个字典的键和值合并到另一个字典中,且盲目地覆盖相同的键的值。
In [41]:D={"a":1,"b":2,"c":3}
list(D.keys())
list(D.values())
list(D.items())
Out[41]:['a', 'b', 'c']
[1, 2, 3]
[('a', 1), ('b', 2), ('c', 3)]
In [42]:D["e"]=["a","b"]
D
Out[44]:{'a': 1, 'b': 2, 'c': 3, 'e': ['a', 'b']}
In [47]:D2={"d":4,"e":"a"}
D.update(D2)
D
Out[47]:{'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 'a'}
元组
元组是无法修改的(其它对象的)集合。元组不支持任何方法调用,不过具有列表的大多数属性:
- 任意对象的有序集合
- 通过偏移存取
- 属于不可变序列类型
- 固定长度、异构、任意嵌套
- 对象引用的数组
help(tuple):
Help on class tuple in module builtins:
class tuple(object)
| tuple() -> empty tuple
| tuple(iterable) -> tuple initialized from iterable's items
|
| If the argument is a tuple, the return value is the same object.
|
| Methods defined here:
|
| __add__(self, value, /)
| Return self+value.
|
| __contains__(self, key, /)
| Return key in self.
|
| __eq__(self, value, /)
| Return self==value.
|
| __ge__(self, value, /)
| Return self>=value.
|
| __getattribute__(self, name, /)
| Return getattr(self, name).
|
| __getitem__(self, key, /)
| Return self[key].
|
| __getnewargs__(...)
|
| __gt__(self, value, /)
| Return self>value.
|
| __hash__(self, /)
| Return hash(self).
|
| __iter__(self, /)
| Implement iter(self).
|
| __le__(self, value, /)
| Return self<=value.
|
| __len__(self, /)
| Return len(self).
|
| __lt__(self, value, /)
| Return self<value.
|
| __mul__(self, value, /)
| Return self*value.n
|
| __ne__(self, value, /)
| Return self!=value.
|
| __new__(*args, **kwargs) from builtins.type
| Create and return a new object. See help(type) for accurate signature.
|
| __repr__(self, /)
| Return repr(self).
|
| __rmul__(self, value, /)
| Return self*value.
|
| count(...)
| T.count(value) -> integer -- return number of occurrences of value
|
| index(...)
| T.index(value, [start, [stop]]) -> integer -- return first index of value.
| Raises ValueError if the value is not present.
元组应用实例
元组的不可变性只适用于元组本身顶层而并非其内容。
x=(5)
y=(5,)
print(type(x),type(y),type(z))
# <class 'int'> <class 'tuple'> <class 'tuple'>