1.元素去重
set()可以实现去重功能,不是排序的。
>>> a = [1, 1, 2, 1, 3, 0, -2, 5, -3]
>>> set(a)
{0, 1, 2, 3, 5, -3, -2}
求两个集合的差。
>>> a = [1, 2, 3, 4]
>>> b = [0, 1, 3, 5]
>>> a.difference(b)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'difference'
>>> set(a).difference(set(b))
{2, 4}
去除空值
filter可以去除诸如:False,None,0,""这样的空值。
>>> a = [0, 1, False, 'a', '', None]
>>> filter(bool, a)
<filter object at 0x7fab05b1e520>
>>> list(filter(bool, a))
[1, 'a']
zip使用
>>> a = [['a', 'A'], ['b', 'B'], ['c', 'C']]
>>> zip(*a)
<zip object at 0x7fab05adfac0>
>>> list(zip(*a))
[('a', 'b', 'c'), ('A', 'B', 'C')]
# 实现矩阵转置
>>> a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> [list(i) for i in zip(*a)]
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]
定义一个函数以按照这个大小切割列表。
>>> from math import ceil
>>> def chunk(lst, size):
... return list(map(lambda x: lst[x * size:x * size + size], \
list(range(0, ceil(len(lst) / size)))))
...
>>> chunk([1,2,3,4,5],2)
[[1, 2], [3, 4], [5]]
将列表的嵌套展开为单个列表。
>>> def spread(arg):
... ret = []
... for i in arg:
... if isinstance(i, list):
... ret.extend(i)
... else:
... ret.append(i)
... return ret
...
>>> spread([1, [2], [[3], 4], 5])
[1, 2, [3], 4, 5]
>>> def deep_flatten(lst):
... result = []
... result.extend(spread(list(map(lambda x: deep_flatten(x) \
if type(x) == list else x, lst))))
... return result
...
>>> deep_flatten([1, [2], [[3], 4], 5])
[1, 2, 3, 4, 5]
打乱列表
from copy import deepcopy
from random import randint
def shuffle(lst):
temp_lst = deepcopy(lst)
m = len(temp_lst)
while (m):
m -= 1
i = randint(0, m)
temp_lst[m], temp_lst[i] = temp_lst[i], temp_lst[m]
return temp_lst
foo = [1,2,3]
shuffle(foo) # [2,3,1]
定义一个函数以按照这个大小切割列表。
>>> from math import ceil
>>> def chunk(lst, size):
... return list(map(lambda x: lst[x * size:x * size + size], \
list(range(0, ceil(len(lst) / size)))))
...
>>> chunk([1, 2, 3, 4, 5], 2)
[[1, 2], [3, 4], [5]]
列表偶数求和
>>> a = [1, 2, 3, 4]
>>> sum([i for i in a])
10
删除列表中元素
[start:end:step],start默认为0,step默认为1
>>> a = [1, 2, 3, 4, 5, 6]
>>> del a[1::2]
>>> a
[1, 3, 5]
?多变量赋值
>>> a, b, c = [1, 2, 3, 4, 5]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: too many values to unpack (expected 3)
>>> a, b, *c = [1, 2, 3, 4, 5]
>>> a, b, c
(1, 2, [3, 4, 5])
>>> a
1
>>> b
2
>>> c
[3, 4, 5]
创建列表和集合
>>> str = [("hello " + i) for i in ['Jone', 'Lucy', 'Jack']]
>>> str
['hello Jone', 'hello Lucy', 'hello Jack']
>>> nums = {i**2 for i in range(10)}
>>> nums
{0, 1, 64, 4, 36, 9, 16, 49, 81, 25}
>>> nums = {i**2 for i in range(10) if i%2==0}
>>> nums
{0, 64, 4, 36, 16}
查看元素是否在列表中
>>> num = 7
>>> res = 'in' if num in [1, 2, 3, 7]
File "<stdin>", line 1
res = 'in' if num in [1, 2, 3, 7]
^
SyntaxError: invalid syntax
>>> res = 'in' if num in [1, 2, 3, 7] else 'not in'
>>> res
'in'
计算阶乘
>>> import math
>>> math.factorial(5)
120
lambda使用
>>> max = lambda x,y: x if x > y else y
>>> max(2, 5)
5
随机选择
>>> import random
>>> res = random.choice(['pros', 'cons'])
>>> res
'pros'
文本替换
>>> s = "python is a good language. I love python!"
>>> s.replace("python", "java")
'java is a good language. I love java!'
|