python中根据某个list排序另一个list

https://s tackoverflow.com

/questions/6618515/sorting-list-based-on-values-from-another-list

比如有如下两个list:

people =['Jim','Pam','Micheal','Dwight']

ages =[27,25,4,9]

按ages排序

方法一:

[x for _,x in sorted(zip(ages,people))]

方法二:

import numpy as np

people=np.array(people)

ages=np.array(ages)

indx=ages.argsort()

sortedpeople=people[indx]

方法三:

sorted_index=sorted(range(len(age)),key=lambda x:people[x])

[people[i] for i in sorted_index]



顺便鄙视一下百度和墙。

python多任务,线程池

25个线程要做完1000个任务

import threading,time,random,os,threadpool
from shutil import copyfile

def dealing(cata):
    print threading.current_thread().name
    print 'entering ',cata
    os.popen('sh DORFTN '+cata).read()
#    os.chdir('..')

if __name__ == "__main__":
    cata=[c for c in os.listdir('.') if os.path.isdir(c)]
   pool=threadpool.ThreadPool(25)
   requests=threadpool.makeRequests(dealing,cata)
    [pool.putRequest(req) for req in requests]
    pool.wait()


如果函数内有多个参数,可如下处理:
import threadpool, os, glob

out=[]

def processing(sample,trace):
   tmp=sample.split('/')
   tracefile=trace+'/'+tmp[-1]
   ret=os.popen('./doublet_search %s %s' % (sample,tracefile)).read()
    print(ret)

if __name__=='__main__':
   traces=glob.glob('Trace/2*')
   samples=glob.glob('Template/2*')
    arglist=[]
    for sample in samples:
       Zs=glob.glob(sample+'/*.?HZ')
       for Z in Zs:
          sam=Z[0:-1]
           for trace in traces:
             arglist.append(([sam,trace],None))
#    print(arglist)
   pool_t=threadpool.ThreadPool(8)
   requests=threadpool.makeRequests(processing,arglist)
    [pool_t.putRequest(req) for req in requests]
    pool_t.wait()

****注:一般在函数中不要用到os.chdir,因为chdir是全局的,如果要用,则要用绝对路径。

如何把包含中文字体的matplotlib图件保存为pdf文件

import matplotlib as mpl
from matplotlib.backends.backend_pdf import PdfPages

mpl.rcParams['pdf.fonttype']=42
mpl.rcParams['font.family']='WenQuanYi Zen Hei'

with PdfPages('test.pdf') as pdf:
xxxx # plot some figure
pdf.savefig()

#questions/45870858/runtimeerror-failed-to-open-truetype-font-in-matplotlib-backends-pdf-in-python stackoverflow