解包
def func(a, *args, **kargs):
print('a ', a)
print('*args ', args)
print('**kargs ', kargs)
func(1, 2, 3, 4, k1 = 1, k2 = 2, k3 = 3)
#a 1
#*args (2, 3, 4)
#**kargs {'k1': 1, 'k2': 2, 'k3': 3}
zip(*A)
def zip(*iterables):
# zip('ABCD', 'xy') --> Ax By
sentinel = object()
iterators = [iter(it) for it in iterables]
while iterators:
result = []
for it in iterators:
elem = next(it, sentinel)
if elem is sentinel:
return
result.append(elem)
yield tuple(result)
转置问题:
res0 = [[0] * row] * colum
res = [[0] * row for _ in range(colum)]
python list属性方法:
1.list.append() #在列表后面追加元素
2.list.extend() #在列表后面追加另一个列表的元素
3.list.pop(index) #弹出指定位置的元素
4.list.sort(reverse=False) #列表元素升序 #reverse=True为降序
5.list.count(data) #统计列表中出现data的次数
6.list.index(data) #列表中元素的索引
7.max(list) #列表中的最大值
8.min(list) #列表中的最小值
9.list.remove(obj) #删除列表中指定元素
10.list.reverse() #列表元素倒序输出
11.list.insert(index,obj) #向指定位置插入元素
python常用技巧:
https://www.cnblogs.com/cangshuchirou/p/8678197.html
python集合SET:
add() 为集合添加元素
clear() 移除集合中的所有元素
copy() 拷贝一个集合
difference() 返回多个集合的差集
difference_update() 移除集合中的元素,该元素在指定的集合也存在。
discard() 删除集合中指定的元素
intersection() 返回集合的交集
intersection_update() 删除集合中的元素,该元素在指定的集合中不存在。
isdisjoint() 判断两个集合是否包含相同的元素,如果没有返回 True,否则返回 False。
issubset() 判断指定集合是否为该方法参数集合的子集。
issuperset() 判断该方法的参数集合是否为指定集合的子集
pop() 随机移除元素
remove() 移除指定元素
symmetric_difference() 返回两个集合中不重复的元素集合。
symmetric_difference_update() 移除当前集合中在另外一个指定集合相同的元素,并将另外一个指定集合中不同的元素插入到当前集合中。
union() 返回两个集合的并集
update() 给集合添加元素
参考链接:https://www.cnblogs.com/zjoe-life/p/10526361.html
找出列表出现次数最多的数:
print(max(set(test), key=test.count))
递归次数的上限一般为1000,重置方法如下:
sys.setrecursionlimit(x)
将两数组的对应位置结合起来:
dict (zip(t1,t2))
不用循环创建一个列表:
import more_itertools
test = [[-1, -2], [1, 2, 3, [4, (5, [6, 7])]], (30, 40), [25, 35]]
print(list(more_itertools.collapse(test)))