python基础查漏补缺4--数据结构

1. type,查看对象或变量的数据类型

>>> type('a')
<class 'str'>
>>> type(5)
<class 'int'>
>>> type(5.0)
<class 'float'>
>>> type(None)
<class 'NoneType'>
>>> type(type)
<class 'type'>
>>> type(print)
<class 'builtin_function_or_method'>

2. 序列的拼接, + *

>>> [1,2,3] * 5
[1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]
>>> [[1,2,3] * 5]*3
[[1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3], [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3], [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]]
>>> [1, 2, 3] + [3, 4, 5]
[1, 2, 3, 3, 4, 5]

3. 元组函数

函 数 名 返 回 值
x in tup 如果 x 是元组 tup 的一个元素,则返回 True,否则返回 False
len(tup) 元组 tup 包含的元素数
tup.count(x) 元组 x 在元组 tup 中出现的次数
tup.index(x) 元组 tup 中第一个元素 x 的索引;如果 x 未包含在tup中,则引发 ValueError 异常
>>> 1 in (1, 2, 3)
True
>>> len((1,))
1
>>> len((1, 2, 3))
3
>>> ((1, 2, 3, 1)).count(1)
2
>>> ((1, 2, 3)).index(2)
1
>>> ((1, 2, 3)).index(4)
Traceback (most recent call last):
  File "<pyshell#163>", line 1, in <module>
    ((1, 2, 3)).index(4)
ValueError: tuple.index(x): x not in tuple

4. 列表函数

函 数 名 返 回 值
s.append(x) 在列表 s 末尾添加元素 x
s.count(x) 返回元素 x 在列表 s 中出现的次数
s.extend(lst) 将lst的所有元素都添加到列表 s 的末尾
s.index(x) 返回第一个 x 元素的索引
s.insert(i, x) 将元素 x 插入到索引 i 指定的元素前面,结果是 s[i] == x
s.pop(i)

删除并返回 s 中索引为 i 的元素

s.remove(x) 删除 s 中的第一个 x 元素
s.reverse() 反转 s 中元素的排列顺序
s.sort() 将 s 中的元素按升序排列
>>> old = [1, 2, 3]
>>> old.append(4)
>>> old
[1, 2, 3, 4]
>>> old.count(1)
1
>>> old.extend([4, 5, 6])
>>> old
[1, 2, 3, 4, 4, 5, 6]
>>> old.index(4)
3
>>> old.insert(4, 8)
>>> old
[1, 2, 3, 4, 8, 4, 5, 6]
>>> old.pop(4)
8
>>> old
[1, 2, 3, 4, 4, 5, 6]
>>> old.remove(4)
>>> old
[1, 2, 3, 4, 5, 6]
>>> old.reverse()
>>> old
[6, 5, 4, 3, 2, 1]
>>> old.sort()
>>> old
[1, 2, 3, 4, 5, 6]

5. 列表解析

>>> [n * n for n in range(1,11)]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
>>> names = ['al', 'mei', 'jo', 'jel']
>>> [n.capitalize() for n in names]
['Al', 'Mei', 'Jo', 'Jel']
>>> nums = [-1, 0, 6, 0, -4, -2, 3]
>>> [n for n in nums if n > 0]
[6, 3]
>>> s = 'Apple Sauce'
>>> ''.join(c for c in s if c.lower() not in 'aeiou')
'ppl Sc'

6. 字典函数

函 数 名 返回值
d.items() 返回一个由字典 d 的键 - 值对组成的视图( view )
d.keys() 返回一个由字典 d 的键组成的视图
d.values() 返回一个由字典 d 的值组成的视图
d.get(key) 返回与 key 相关联的值
d.pop(key) 删除 key 键并返回与之相关联的值
d.popitem() 删除字典 d 中的某个 键 - 值对并返回对应的键 - 值
d.clear() 删除字典 d 中的所有元素
d.copy() 复制字典d
d.fromkeys(s, t) 创建一个新字典,其中的键来自 s, 值来自 t
d.setdefault(key, v) 如果键 key 包含在字典 d 中,则返回其值;否则,返回 v 并将 (key, v)添加到字典 d 中
d.update(e) 将 e 中的键 - 值对添加到字典 d 中;e 可能是字典,也可能是键 - 值对序列
>>> d = {'blue' : 2}
>>> new_d = d.fromkeys(['orange', 'green', 'red'], 3)
>>> new_d
{'green': 3, 'orange': 3, 'red': 3}
>>> d = new_d.copy()
>>> d
{'green': 3, 'orange': 3, 'red': 3}
>>> d.items()
dict_items([('green', 3), ('orange', 3), ('red', 3)])
>>> d.keys()
dict_keys(['green', 'orange', 'red'])
>>> d.values()
dict_values([3, 3, 3])
>>> d.get('green')
3
>>> d.setdefault('green', 4)
3
>>> d
{'green': 3, 'orange': 3, 'red': 3}
>>> d.setdefault('white', 5)
5
>>> d
{'white': 5, 'green': 3, 'orange': 3, 'red': 3}
>>> d.update({'black': 4})
>>> d
{'white': 5, 'green': 3, 'orange': 3, 'red': 3, 'black': 4}
>>> d.pop('green')
3
>>> d
{'white': 5, 'orange': 3, 'red': 3, 'black': 4}
>>> d.popitem()
('white', 5)
>>> d
{'orange': 3, 'red': 3, 'black': 4}
>>> d.clear()
>>> d
{}
原文地址:https://www.cnblogs.com/yunmenzhe/p/7511745.html