python小技巧总结

 

 

1 math.ceil()  #向上取整

   math.floor()  #向下取整

   round(a,2)  #a四舍五入,保留2位小数

   int()  #向下取整 ,返回整数类型

   math.sqrt()  #开根号

  pow(a,x) #求a的x次方

2 遍历字典

https://www.cnblogs.com/stuqx/p/7291948.html

3 字典根据value进行简单的排序

 

d = {'a':1,'b':4,'c':2}
a=sorted(d.items(),key = lambda x:x[1],reverse = True)
print(a)

输出:[('b', 4), ('c', 2), ('a', 1)]

 

4 复杂list排序

忽略大小写字符串排序:

>>> sorted(['bob', 'about', 'Zoo', 'Credit'], key=str.lower)
['about', 'bob', 'Credit', 'Zoo']

 

students = [('john', 'W', 1), ('jane', 'B', 12), ('dave', 'B', 1)]
a=sorted(students, key=operator.itemgetter(1,2))
print(a)

输出:[('dave', 'B', 1), ('jane', 'B', 12), ('john', 'W', 1)]

 

若更复杂排序,需要自定义函数:

from functools import cmp_to_key

#前2项升序,后1项降序
def cmp1(a,b):
   if a[0]<b[0]:
       return -1
   elif a[0]>b[0]:
       return 1
   else:
       if a[1]>b[1]:
           return  1
       elif a[1]<b[1]:
           return -1
       else:
           if a[2]>b[2]:
                return -1
           elif a[2]<b[2]:
               return 1
           else:
               return 0

a=[['b','d',3],['a','d',4],['b','d',6],['a','e',6]]
a.sort(key=cmp_to_key(cmp1))
print(a)

输出:

[['a', 'd', 4], ['a', 'e', 6], ['b', 'd', 6], ['b', 'd', 3]]

5 排列、组合

 

from itertools import permutations
from itertools import combinations

for i in permutations([1, 2, 3], 3):
    print(i)

for i in combinations([1, 2, 3], 2):
    print(i)

输出:(1, 2, 3)

(1, 3, 2)
(2, 1, 3)
(2, 3, 1)
(3, 1, 2)
(3, 2, 1)
(1, 2)
(1, 3)
(2, 3)

注意返回类型:

 

a=permutations([1, 2, 3])
print(type(a))
print(list(a))

输出:

<class 'itertools.permutations'>

[(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]

 

6 注意numpy广播机制

普通的list相加是元素的并,np的加是分别加:

a = np.random.randn(2, 3) # a.shape = (2, 3)
b = np.random.randn(2, 1) # b.shape = (2, 1)
print(a)
print(b)
c = a + b
print(c)

aaa=[1,2]
bbb=[4,3]
print(aaa+bbb)

输出:

[[ 0.22439514 -1.46048723 -0.09989994]
 [-0.16181647  0.89533506 -0.02419727]]
[[-1.98479857]
 [-0.45767346]]
[[-1.76040344 -3.4452858  -2.08469852]
 [-0.61948993  0.4376616  -0.48187073]]
[1, 2, 4, 3]

 

普通list的交集:

a=[2,3,4,5]
b=[2,5,8]
tmp = [val for val in a if val in b]

 

a = np.random.randn(3, 3)
b = np.random.randn(3, 1)
c = a*b
print(c)

输出

[[ 0.31930559  0.57259555  0.3371194 ]
 [-0.55700812 -0.49222884  0.57338674]
 [-0.67481986 -0.16179442  1.52381768]]

 

7 python多维切片之冒号和三个点

https://blog.csdn.net/z13653662052/article/details/78010654/

8 双重for生成list用法:

[m + n for m in 'ABC' for n in 'XYZ']
#输出
#['AX', 'AY', 'AZ', 'BX', 'BY', 'BZ', 'CX', 'CY', 'CZ']

 

9 函数参数前加*的用法:

def f(*args):
    for arg in args:    # 取出tuple中的每个元素,然后打印
        print(arg)

f(1,2,3,4)
#输出
1
2
3
def f(**kargs):
    print(kargs)

f(a=1, b=2)
#输出
{'a': 1, 'b': 2}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值