列表通常包含多个元素,所以在命名的时候会使用一个复数的形式。
List
增
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
# ['honda', 'yamaha', 'suzuki']
# 末尾添加 append
motorcycles.append('ducati')
print(motorcycles)
# ['honda', 'yamaha', 'suzuki', 'ducati']
# 插入 insert
motorcycles.insert(0, 'ducati')
print(motorcycles)
# ['ducati', 'honda', 'yamaha', 'suzuki', 'ducati']
# 两个list相加 +相当于extend
>>> a = [1]
>>> b = [2]
>>> a+b
[1, 2]
>>> a.append(b)
>>> a
[1, [2]]
>>> a.extend(b)
>>> a
[1, [2], 2]
删
motorcycles = ['honda', 'yamaha', 'suzuki', 'ducati', 'suzuki', 'jialing', 'lingmu']
print(motorcycles)
# 只需要删除 del
del motorcycles[0]
print(motorcycles)
# 删除同时需要拿到元素值 pop
motorcycles.pop(0)
print(motorcycles)
# pop默认是删除并拿到末尾元素,也可以指定位置 motorcycles.pop(0)
motorcycles.pop()
print(motorcycles)
# 根据值删除 remove,只删除第一个指定的值,如果删除多个,需要循环
motorcycles.remove('suzuki')
print(motorcycles)
['honda', 'yamaha', 'suzuki', 'ducati', 'suzuki', 'jialing', 'lingmu']
['yamaha', 'suzuki', 'ducati', 'suzuki', 'jialing', 'lingmu']
['suzuki', 'ducati', 'suzuki', 'jialing', 'lingmu']
['suzuki', 'ducati', 'suzuki', 'jialing']
['ducati', 'suzuki', 'jialing']
改
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
motorcycles[0] = 'ducati'
print(motorcycles)
['honda', 'yamaha', 'suzuki']
['ducati', 'yamaha', 'suzuki']
查
- 取任意一个
- 取最后一个
bicyclkes = ['trek', 'cannondale', 'redline', 'specialized']
print(bicyclkes[0])
print(bicyclkes[-1])
trek
specialized
组织列表
排序
list.sort()
:改变原列表sorted(list)
:生成一份拷贝,不改变原列表
# sort 改变原列表
cars = ['bmw', 'audi', 'toyota', 'sabaru', 'mazida']
cars.sort()
print(cars)
cars.sort(reverse=True)
print(cars)
-- output
['audi', 'bmw', 'mazida', 'sabaru', 'toyota']
['toyota', 'sabaru', 'mazida', 'bmw', 'audi']
# sorted 生成一份拷贝,不改变原列表
cars = ['bmw', 'audi', 'toyota', 'sabaru']
print("Here is the original list:")
print(cars)
print("\nHere is the sorted list:")
print(sorted(cars))
-- output
Here is the original list:
['bmw', 'audi', 'toyota', 'sabaru']
Here is the sorted list:
['audi', 'bmw', 'sabaru', 'toyota']
# 倒着打印列表
cars = ['bmw', 'audi', 'toyota', 'sabaru']
cars.reverse()
print(cars)
# 确定列表长度
print(len(cars))
-- output
['sabaru', 'toyota', 'audi', 'bmw']
4
去重
- 使用set
- 使用itertools
- 不改变原顺序
# 1. set()
ids = [1,4,3,3,4,2,3,4,5,6,1]
ids = list(set(ids))
# 2. 使用itertools
import itertools
ids = [1,4,3,3,4,2,3,4,5,6,1]
it = itertools.groupby(ids)
for k, g in it:
print k
# 3. 不改变原顺序
In [5]: ids = [1,4,3,3,4,2,3,4,5,6,1]
In [6]: func = lambda x,y: x if y in x else x + [y]
In [7]: reduce(func, [[], ] + ids)
Out[7]: [1, 4, 3, 2, 5, 6]
展平元素
例如:[[1,2,3], [4,5,6]] -> [1,2,3,4,5,6]
- 循环
```python
The list of lists
[[0, 1, 2, 3], [0, 1, 2, 3, 4, 5, 6]]
list_of_lists = [range(4), range(7)] flattened_list = []
flatten the list
for x in list_of_lists: for y in x: flattened_list.append(y)
2. 列表
```python
#The list of lists
list_of_lists = [range(4), range(7)]
#flatten the lists
flattened_list = [y for x in list_of_lists for y in x]
- extend
获取出现次数最多的元素
- 使用collections
from collections import Counter
a = [['PER', 0, 1], ['PER', 5, 8], ['ORG', 0, 3]]
Counter([x[0] for x in a])
-- output
Counter({'PER': 2, 'ORG': 1})
- 使用numpy
import numpy
num=1000000
lst = np.random.randint(num / 10, size=num)
dict(zip(*np.unique(lst, return_counts=True)))
- 使用list.count()
import numpy
num=1000000
lst = np.random.randint(num / 10, size=num)
dic = {}
for i in lst:
dic[i] = lst.count(i)
- 获取字典中value的最大值所对应的键的方法
lt = ['小马', '小敏', '小乔', '小敏', '小杜', '小杜', '小孟', '小敏']
print(max(lt, key=lt.count)
查找指定值在列表中的位置
# [].index 返回列表中第一次出现的位置
lt = ['小马', '小敏', '小乔', '小敏', '小杜', '小杜', '小孟', '小敏']
lt.index('小敏')
-- output
1
从列表中随机挑选N个元素
sample(population, k)从population中取样,一次取k个,返回一个k长的列表。
from random import sample
a = [3, 4, 5, 6]
sample(a, 2)
-- output
[5, 3]
遍历列表
for 循环遍历
magicians = ['alice', 'david', 'carolina']
for magician in magicians:
print(magician)
alice
david
carolina
指定索引遍历
l = [2,7,11,15]
for index,value in enumerate(l):
print index,value
创建数值列表
# 生成一系列数字
for value in range(1, 5):
print(value)
# 使用range()创建数字列表
numbers = list(range(1, 6))
print("numbers =", numbers)
# 指定步长
even_numbers = list(range(2, 11, 2))
print("even_numbers =", even_numbers)
1
2
3
4
numbers = [1, 2, 3, 4, 5]
even_numbers = [2, 4, 6, 8, 10]
对数字列表执行统计计算
digits = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print("min_digits =", min(digits))
print("max_digits =", max(digits))
print("sum_digits =", sum(digits))
min_digits = 1
max_digits = 10
sum_digits = 55
列表解析
列表解析是定义一个表达式,生成的值存入到列表里
squares = [value ** 2 for value in range(1, 11)]
print(squares)
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
使用列表的一部分
切片
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print("players[0:3] =", players[0:3])
print("players[:4] =", players[:4])
print("players[2:] =", players[2:])
print("players[-3:] =", players[-3:])
players[0:3] = ['charles', 'martina', 'michael']
players[:4] = ['charles', 'martina', 'michael', 'florence']
players[2:] = ['michael', 'florence', 'eli']
players[-3:] = ['michael', 'florence', 'eli']
复制列表
直接赋值friends_foods=my_foods
只是让两个变量同时指向一个对象,对任意一个变量的修改,都会引起内容的修改。
my_foods = ['pizza', 'falafel', 'carrot cake']
friends_foods = my_foods[:]
print("My favorite foods are:")
print(my_foods)
print("\nMy friends's favorite foods are:")
print(friends_foods)
my_foods.append('cannoli')
friends_foods.append('ice cream')
print("After add, My favorite foods are:")
print(my_foods)
print("\nAfter add, My friends's favorite foods are:")
print(friends_foods)
My favorite foods are:
['pizza', 'falafel', 'carrot cake']
My friends's favorite foods are:
['pizza', 'falafel', 'carrot cake']
After add, My favorite foods are:
['pizza', 'falafel', 'carrot cake', 'cannoli']
After add, My friends's favorite foods are:
['pizza', 'falafel', 'carrot cake', 'ice cream']
元组
元组是不可变的列表,切片与列表是一致的。
字典 dict
创建/增加
# 可以在最后一个键值对后也添加逗号,方便以后继续添加
>>> favorite_languages = {
... 'jen':'python',
... 'sarah':'c',
... 'edward':'ruby',
... 'phil':'python',
... }
>>> favorite_languages
{'sarah': 'c', 'edward': 'ruby', 'jen': 'python', 'phil': 'python'}
# 从两个列表创建字典
>>> a=['a', 'b', 'c']
>>> b=3
>>> c=dict(zip(a, range(b)))
>>> c
{'a': 0, 'b': 1, 'c': 2}
根据key或value排序
>>> a = {'a':1, 'b':3, 'c':2}
>>> a
{'a': 1, 'b': 3, 'c': 2}
# 根据key排序,默认为顺序
>>> sorted(a.items(), key=lambda item: item[0])
[('a', 1), ('b', 3), ('c', 2)]
# 根据value排序,倒序
>>> sorted(a.items(), key=lambda item: item[1], reverse=True)
[('b', 3), ('c', 2), ('a', 1)]
返回默认key的字典 defaultdict
defaultdict 超级好用,他无需判断某个key是否在字典里,当不在时,会自动加入,对应的value,在不同的类型时,有不同的默认值:
- int - 0
- list - []
- set - set()
- str - 空字符串
```python
from collections import defaultdict d1 = dict() d2 = defaultdict(list) print(d1[‘a’]) Traceback (most recent call last): File “
“, line 1, in KeyError: ‘a’
print(d2[‘a’]) []
d3 = defaultdict(int) d3[‘a’] 0 d2[‘a’].append(1) d2 defaultdict(
, {‘a’: [1]}) ``` update
#!/usr/bin/python
>>> d1 = {'Name': 'Zara', 'Age': 7}
>>> d2 = {'Sex': 'female', 'Name': 'Sarah' }
>>>
>>> d1.update(d2)
>>> print("Value : %s" % d1)
Value : {'Name': 'Sarah', 'Age': 7, 'Sex': 'female'}
删除
d = {"name":"wasan", "age":14}
del d["name"]
-- 结果
{'age': 14}
遍历
- 迭代keys
map.keys()
- 迭代values
map.values()
- 同时迭代key和value
map.items()
- 按顺序遍历字典的key
sorted(map.keys)
```pythona = {“name”:”zhangsan”, “age”:18, “gender”:”male”} a.keys() [‘gender’, ‘age’, ‘name’]
a.values() [‘male’, 18, ‘zhangsan’]
a.items() [(‘gender’, ‘male’), (‘age’, 18), (‘name’, ‘zhangsan’)]
for k in a.keys(): … print(k, a[k]) … (‘gender’, ‘male’) (‘age’, 18) (‘name’, ‘zhangsan’)
for k,v in a.items(): … print(k,v) … (‘gender’, ‘male’) (‘age’, 18) (‘name’, ‘zhangsan’)
sorted(a.keys()) [‘age’, ‘gender’, ‘name’] ```