1.合并字典
eg:
>>> test1={'a':23,'b':56,'e':'s'}
>>> test2={'k':45,'p':0}
python3:
>>> {**test1,**test2}
{'a': 23, 'b': 56, 'e': 's', 'k': 45, 'p': 0}
python2:
>>> test1={'a':23,'b':56,'e':'s'}
>>> test2={'k':45,'p':0}
>>> result=dict( test1.items() + test2.items())
>>> result
{'a': 23, 'p': 0, 'k': 45, 'b': 56, 'e': 's'}
python3_python2:
>>> merge_params = test1.copy()
>>> merge_params.update(test2)
{'a': 23, 'b': 56, 'e': 's', 'k': 45, 'p': 0}
>>> dictMerged2 = dict( test1, **test2 )
>>> dictMerged2
{'a': 23, 'p': 0, 'k': 45, 'b': 56, 'e': 's'}
2.列表的合并
例如有如下:
raw=['[1,2]','[3,4]','[4,5]']
# 合并结果为一个大列表
res_li=[1,2,3,4,5]
常规操纵
temp_li = []
for res in raw:
temp_li.extend(eval(res))
使用+号
temp_li = []
for res in raw:
temp_li+=eval(res)
使用切片
list_a = ['a','b','c']
list_b = ['d','e','f','g']
list_a[0:0] = list_b #列表中[n,n],表示在列表的第n+1个位置,将对应列表逐个元素插入合并
print(list_a)
#千万不能写成 list_a[0] = list_b,这会使得list_a[0]变为一个列表,而不是列表内的元素。
>>> ['d', 'e', 'f', 'g', 'a', 'b', 'c']
也可以修改位置,灵活变更合并的序列在原列表中的位置:
list_a = ['a','b','c']
list_b = ['d','e','f','g']
list_a[-1:-1] = list_b #合并至最末尾
print(list_a)
>>> ['a', 'b', 'd', 'e', 'f', 'g', 'c']
合并到第二个位置:
list_a = ['a','b','c']
list_b = ['d','e','f','g']
list_a[1:1] = list_b #合并至第二个位置
print(list_a)
>>> ['a', 'd', 'e', 'f', 'g', 'b', 'c']
列表推导式
res=[x for j in raw for x in eval(j)]
import json
res=[x for j in raw for x in json.loads(j)]
使用itertools
import itertools
list(itertools.chain(*map(eval, raw)))
“指针”/解包操作
Python >= 3.5 PEP 448中可以使用*来进行类似指针的操作:
list_a = ['a','b','c']
list_b = ['d','e','f','g']
list_ab = [*list_a,*list_b]
print(list_ab)
>>> ['a', 'b', 'c', 'd', 'e', 'f', 'g']
3.列表取样
通过使用random软件库,以下代码从给定的列表中生成了n个随机样本。
>>> import random
>>> my_list = ['a', 'b', 'c', 'd', 'e']
>>> num_samples = 2
>>> samples = random.sample(my_list,num_samples)
>>> print(samples)
['e', 'a']
>>>
4.列表清单扁平化
有时你不确定列表的嵌套深度,而且只想全部要素在单个平面列表中。
from iteration_utilities import deepflatten
# if you only have one depth nested_list, use this
def flatten(l):
return [item for sublist in l for item in sublist]
l = [[1,2,3],[3]]
print(flatten(l))
# [1, 2, 3, 3]
# if you don't know how deep the list is nested
l = [[1,2,3],[4,[5],[6,7]],[8,[9,[10]]]]
print(list(deepflatten(l, depth=3)))
# [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
5.列表排列组合
from itertools import combinations
from itertools import product
def test_func1(num_list):
'''
生成排列
列表中元素不允许重复出现
排列数计算为:n!,其中n为num_list列表中元素个数
'''
tmp_list = itertools.permutations(num_list)
res_list = []
for one in tmp_list:
res_list.append(one)
print(res_list)
print('元素不允许重复出现排列总数为:', len(res_list))
def test_func11(num_list):
'''
生成排列
列表中元素可以重复出现
排列总数计算为:(n*n*n...*n),一共n个n相乘
'''
num = len(num_list)
res_list = list(product(num_list, repeat=num))
print(res_list)
print('元素可以重复出现排列总数为:', len(res_list))
def test_func2(num_list):
'''
生成组合,不限元素个数
列表中元素不允许重复出现
组合数计算为:2^n,其中n为num_list列表中元素个数
'''
res_list = []
for i in range(len(num_list) + 1):
res_list += list(combinations(num_list, i))
print(res_list)
print('元素不允许重复出现组合总数为:', len(res_list))
def test_func22(num_list):
'''
生成组合,不限元素个数
列表中元素可以重复出现
'''
res_list = []
num_list1 = [str(i) for i in num_list]
for i in range(0, len(num_list) + 1):
res_list += [''.join(x) for x in itertools.product(*[num_list1] * i)]
print(res_list)
print('元素可以重复出现组合总数为:', len(res_list))
if __name__ == '__main__':
num_list = [1, 2, 3, 4]
test_func1(num_list)
print('-------------------------------------')
test_func11(num_list)
print('-------------------------------------')
test_func2(num_list)
print('-------------------------------------')
6. 使用标题类(首字母大写)
以下代码可用于将字符串转换为标题类。这是通过使用字符串类中的title()方法来完成。
my_string = "my name is chaitanya baweja"
# using the title() function of string class
new_string = my_string.title()
print(new_string)
# Output
# My Name Is Chaitanya Baweja