- 工作路径
import os
os.chdir("E:\PyCharm\python_pycharm") #修改当前工作目录
path = os.getcwd() #获取当前工作目录
print (path)
- 计数排序
方法1
import pandas as pd
a= pd.value_counts(req)
输出:
47 5
45 5
方法2
d= Counter(req)
输出:Counter({47: 5, 43: 5, 45: 5, 8: 5, 27: 5,32: 4,
req_size=[]
for k,v in a.items():
req_size.append(k)
req_size.append(v)
req_size_reshpe= np.reshape(req_size,[-1,2])
- 读取最大值
grid= [[3,0,8,4],[2,4,5,7],[9,2,6,3],[0,3,1,0]]
max_row= [max(row)for rowin grid]
max_col= [max(col)for colin zip(*grid)]
print (max_row,max_col)
- range() 函数
range()函数返回的是一个range对象。不过可以使用list()函数将其转换成list对象。range是一个可迭代的对象,可以使用for循环迭代输出。
a = list(range(1,6))
print(a)
输出:
[1, 2, 3, 4, 5]
- zipf分布
matlab
function x = zipf_rand(N, expn, M)
% Generate random numbers based on Zipf distribution
% Author: Tuyen Tran (tuyen.tran@rutgers.edu). Oct 2015
%
% Reference: https://en.wikipedia.org/wiki/Zipf's_law
%
% N Number of Elements
% expn Exponent
% M Number of sample to be generated
%
% Example: zipf_rand(3,1,4)
% ans = 3 2 1 1
if nargin == 2 %只输入两个变量
M = 1;
end
ranks = 1:1:N;
pmf = (ranks.^(-expn))/sum(ranks.^(-expn));
samples = rand(1,M);
p = cumsum(pmf(:));
[~,x] = histc(samples,[0;p/p(end)]);
end