矩阵转换
1 #!/usr/local/bin/python3 2 3 # 通用 4 def transpose_irregular(matrix_s): 5 print('原矩阵为:',matrix_s) 6 matrix_d = [[0 for col in range(len(matrix_s))] for row in range(len(matrix_s[0]))] 7 print(matrix_d) 8 for i in range(len(matrix_s[0])): 9 for j in range(len(matrix_s)): 10 matrix_d[i][j] = matrix_s[j][i] 11 return matrix_d 12 13 # 方阵 14 def transpose_regular(matrix_s): 15 print('原矩阵为:',matrix_s) 16 for i in range(len(matrix_s)): 17 for j in range(i): 18 matrix_s[i][j],matrix_s[j][i]=matrix_s[j][i],matrix_s[i][j] 19 20 return matrix_s 21 22 if __name__ == '__main__': 23 row=int(input('输入矩阵行数:')) 24 col=int(input('输入矩阵列数:')) 25 matrix_s=[[0 for col in range(col)] for row in range(row)] 26 for i in range(row): 27 matrix_s[i] = input('输入第%s行数据:'%i).split(' ') 28 29 if len(matrix_s) == len(matrix_s[0]): 30 matrix_d=transpose_regular(matrix_s) 31 else: 32 matrix_d=transpose_irregular(matrix_s) 33 print('转换后矩阵为:',matrix_d)
统计词频
1 # 方法一 2 content = "" 3 words = content.split() 4 # 数据清洗 5 for i in range(len(words)): 6 words[i] = words[i].strip('.,;') # 去掉标点 7 words[i] = words[i].lower() # 大些转小写 8 9 # 统计 10 for word in words: 11 hist[word] = hist.get(word,0) + 1 12 13 # 排序 14 data = list(hist.items()) 15 data.sort(key=lambda x:x[1],reverse=True) 16 17 18 # 方法二 19 import os 20 from collections import Counter 21 content = '' 22 data_t = Counter(re.split('W+',data)) 23 data_t = data_t.most_common(10) # 统计频率最高的10个词
列表/字典解析 元组
from random import randint data = [randint(-10,10) for x in range(10)] print(data) # fileter 函数 data_f = filter(lambda x: x >= 0, data) print([data for data in data_f]) # 列表解析 data_l = [x for x in data if x >=0] print(data_l) # 字典解析 d = {x: randint(60,100) for x in range(1,21)} print(d) d_s = {k:v for k, v in d.items() if v > 90} print(d_s)
元组namedtuple的使用
from collections import namedtuple Student = namedtuple('Student',['name','age','gender','email']) s = Student('ray',18,'male','raygift@163.com') print(s.name,s.age,s.gender,s.email)
根据字典中值的大小,对字段中项排序
1 from random import randint 2 d = {x:randint(60, 100) for x in 'xyzabc'} 3 print(d) 4 5 # zip 实现 6 temp = zip(d.values(), d.keys()) 7 print(list(temp)) 8 9 # sorted实现 10 temp = sorted(d.items(),key=lambda x: x[1]) 11 print(temp)
找多个字典中的公共键(key)
1 from random import randint, sample 2 3 d1 = {x:randint(1,4) for x in sample('abcdefg', randint(3,6))} 4 d2 = {x:randint(1,4) for x in sample('abcdefg', randint(3,6))} 5 d3 = {x:randint(1,4) for x in sample('abcdefg', randint(3,6))} 6 7 print(d1,d2,d3) 8 print(d1.keys() & d2.keys() & d3.keys()) 9 10 11 from functools import reduce 12 print(reduce(lambda a,b:a &b, map(dict.keys,[d1,d2,d3])))
让字典有序
1 from collections import OrderedDict 2 from time import time 3 from random import randint 4 5 d = OrderedDict() 6 players = list('abcdefgh') 7 start = time() 8 9 10 for i in range(8): 11 input() 12 p = players.pop(randint(0,7 -i)) 13 end = time() 14 print(i+1,p,end-start) 15 d[p] = (i+1,end-start) 16 17 print("-"*30) 18 19 for k in d: 20 print(k,d[k])
历史记录
1 from collections import deque 2 from random import randint 3 4 N = randint(0,100) 5 history = deque([],5) 6 7 def guess(k): 8 9 if k == N: 10 print('right') 11 return True 12 if k < N: 13 print('%s is less than N'%k) 14 else: 15 print('%s is great than N'%k) 16 17 18 while True: 19 line = input('please input a number:') 20 21 if line.isdigit(): 22 k = int(line) 23 history.append(k) 24 if guess(k): 25 break 26 27 elif line == 'history' or line == 'h?': 28 print(list(history)) 29 30 31 import pickle 32 q = [x for x in range(10)] 33 34 pickle.dump(q,open('history','wb+')) 35 print(pickle.load(open('history','rb+')))