python常用技巧

  1、 list深度拷贝

l1 = [1,2,3]
l2 = list(l1)
 
l1 == l2 #True
l1 is l2 #False

  2、input()采集一个字符串 ‘1 4 2 3’,将其转化为list

#方法一
k4=[int(x) for x in input().split()]

#方法二
kk=hh.split()
kk2=list(map(lambda x:int(x),kk))

#方法三 采集固定数内容

[n, k] = [int(x) for x in input().split()]

  3、将一个list,以str形式输出

result=[1,3,2,3,4]
print(' '.join(map(str, result))) 

  4、去前后空格

pp.strip()

  5、二维list排序

#方法一
students = [['john', 'A', 15], ['jane', 'B', 2],['dave', 'B', 10]] students=sorted(students, key=lambda x:x[2])

#方法二
students.sort(key=lambda x:x[2])

  6、list内容求和

sum([1,2,3])

  7、将二维list按矩阵形式输出

import numpy as np
state=[[0, 4, 1], [2, 2, 1], [3, 1, 1], [5, 9, 1]]
list_to_matrix = np.mat(state)    # 列表转矩阵
print(list_to_matrix, end='\n\n')

  8、生成式 生成二维list

state = [[0 for col in range(4)] for row in range(5)]

  9、查看现有库的版本

pip show matplotlib

  

  

 

posted on 2020-02-02 21:19  耀扬  阅读(184)  评论(0编辑  收藏  举报

导航