Best Python code snippet using fMBT_python
按人求od.py
Source:按人求od.py
1from collections import deque #æé«listå é¤æå
¥é度2import operator3import os4import time5yuefenhuansuan={1:['1',0],2:['2',31],3:['3',59],4:['4',90],5:['5',120],6:['6',151],7:['7',181],8:['8',212],9:['9',243],10:['10',273],11:['11',304],12:['12',334]}6d={}7n=08q_sj=249z_sj=2410yue=411for mm in range(yue,yue+1): 12 for dd in range(q_sj,z_sj+1): 13 riqi=yuefenhuansuan[int(mm)][1]+dd14 if len(str(mm)) <2:15 mm='0'+str(mm)16 if len(str(dd))<2:17 dd='0'+str(dd)18 d={}19 d_add={} #add=address20 d_od={}21 l_inforid=deque(['id,家,æä¸ä¹è½¦ç¹,\n'])22 23 n=024 try:25 with open('E:\广å·å
¬äº¤\广å·æ°æ®åèæ¬\shuchu//'+str(mm)+'-'+str(dd)+'//chenggong.csv','r+',encoding='UTF-8')as f:26 for hang in f.readlines():27 n+=128 if n%50000==0:29 print(str(mm),str(dd),':',n)30 if n!=1:31 line=hang.split(',')32 if line[0] not in d:33 d[line[0]]={}34 shijian=line[-2].split(' ')35 nianyueri=shijian[0]36 nyr=nianyueri.split('-')37 shifenmiao=shijian[1].split(':')38 shi=shifenmiao[0]39 fen=shifenmiao[1]40 miao=shifenmiao[2]41 sfm=str(shifenmiao[0]+':'+shifenmiao[1]+':'+shifenmiao[2])42 shiju = int(shi) * 3600 + int(fen) * 60 + int(miao) - 86400 * (riqi - yuefenhuansuan[int(nyr[1])][1]-int(nyr[2])) #å°æ¶é´æ¢æ以æä¸å¤©é¶ç¹ä¸ºåºåï¼ä¸ºä»¥ååæåºåå¤43 d[line[0]][shiju]=hang44 except:45 with open('E:\广å·å
¬äº¤\广å·æ°æ®åèæ¬\shuchu//'+str(int(mm)+1)+'-'+str(1)+'//chenggong.csv','r+',encoding='UTF-8')as f:46 for hang in f.readlines():47 n+=148 if n%50000==0:49 print(str(mm),str(dd),':',n)50 if n!=1:51 line=hang.split(',')52 if line[0] not in d:53 d[line[0]]={}54 shijian=line[-2].split(' ')55 nianyueri=shijian[0]56 nyr=nianyueri.split('-')57 shifenmiao=shijian[1].split(':')58 shi=shifenmiao[0]59 fen=shifenmiao[1]60 miao=shifenmiao[2]61 sfm=str(shifenmiao[0]+':'+shifenmiao[1]+':'+shifenmiao[2])62 shiju = int(shi) * 3600 + int(fen) * 60 + int(miao) - 86400 * (riqi - yuefenhuansuan[int(nyr[1])][1]-int(nyr[2])) #å°æ¶é´æ¢æ以æä¸å¤©é¶ç¹ä¸ºåºåï¼ä¸ºä»¥ååæåºåå¤63 d[line[0]][shiju]=hang64 65 l=[]66 67 for k,v in d.items():68 sorted_kk=sorted(v.items(),key=operator.itemgetter(0))69 for x in sorted_kk:70 l.append(x[1])71 72 73 d={'':{'day':{'qidian':{},'zhongdian':{}},'night':{'qidian':{},'zhongdian':{}}}}74 75 y=0 #æ¯å¦è¿è¡å¤ææ©é«å³°èµ·ç¹ä¾æ®,0为å¤æï¼1为ä¸å¤æ76 yy=0 #æ¯å¦è¿è¡å¤ææé«å³°èµ·ç¹ä¾æ®,0为å¤æï¼1为ä¸å¤æ77 z=1 #æ¯å¦å½å
¥æ©é«å³°ç»ç¹ä¾æ®,0为å½å
¥ï¼1为ä¸å½å
¥78 zz=1 #æ¯å¦å½å
¥æé«å³°ç»ç¹ä¾æ®,0为å½å
¥ï¼1为ä¸å½å
¥79 day_zd='null' #ç½å¤©ç»ç¹80 night_zd='null' #æä¸ç»ç¹81 lastID='' #è®°å½ä¸ä¸idï¼ä¸ºåå
¥zhongdianä½åå¤82 for line in l:83 line=line.split(',')84 shijian=line[-2].split(' ')85 date=nianyueri.split('-')86 shijian=shijian[1].split(':')87 fzj=int(shijian[0])*60+int(shijian[1]) #fenzhongjuï¼ä»¥åé为åä½è·0ç¹88 riqi=yuefenhuansuan[int(date[1])][1]+int(date[2])89 zhandian=line[-3]90 91 if line[0] != lastID:92 rizi=riqi93 y=0 #æ¯å¦è¿è¡å¤ææ©é«å³°èµ·ç¹ä¾æ®94 yy=0 #æ¯å¦è¿è¡å¤ææé«å³°èµ·ç¹ä¾æ®95 #å建起ç¹åç»ç¹çåå
¸ï¼å¹¶åèªæä¸çä¸ä¸çæ¶æ®µåå
¸96 if line[0] not in d:97 d[line[0]]={'day':{'qidian':{},'zhongdian':{}},'night':{'qidian':{},'zhongdian':{}}} 98 #å°ä¸ä¸IDçæ©ææ¶é´æ®µçç»ç¹å½å
¥99 if z==0:100 z=1101 if day_zd in d[lastID]['day']['zhongdian']:102 d[lastID]['day']['zhongdian'][day_zd]+=1103 else:104 d[lastID]['day']['zhongdian'][day_zd]=1 105 if zz==0:106 z=1107 if night_zd in d[lastID]['night']['zhongdian']:108 d[lastID]['night']['zhongdian'][night_zd]+=1109 else:110 d[lastID]['night']['zhongdian'][night_zd]=1111 lastID=line[0]112 113 #å¤ææ©é«å³°èµ·ç¹ï¼å¹¶è®°å½æå¯è½çç»ç¹114 if fzj >=420 and fzj <=540 : #å 为éå¶äºæ¶é´ï¼æå¯è½å¼å§çèµ·ç¹ä¸å¨è¿éï¼å¦æåªæ¯æ³ç¥éå
¶å®¶åºå·¥ä½å°ç¹å¯æ¾å®½æ¶é´æ®µï¼ä½ä¸è½ææ¡å
¶æ¯å¦å¨é«å³°æ¶æ®µåºè¡115 if y==0 :116 y=1117 if zhandian in d[line[0]]['day']['qidian']:118 d[line[0]]['day']['qidian'][zhandian]+=1119 else :120 d[line[0]]['day']['qidian'][zhandian]=1121 122 #å¤ææé«å³°èµ·ç¹ï¼å¹¶è®°å½æå¯è½çç»ç¹123 elif fzj >=1050 and fzj <=1170: 124 if yy==0 :125 #yy=1126 if zhandian in d[line[0]]['night']['qidian']:127 d[line[0]]['night']['qidian'][zhandian]+=1128 else :129 d[line[0]]['night']['qidian'][zhandian]=1130 131 if line[0] == lastID: 132 if riqi==rizi:133 #å¤ææ©é«å³°èµ·ç¹ï¼å¹¶è®°å½æå¯è½çç»ç¹134 if fzj >=420 and fzj <=540 : 135 if y==0 :136 y=1137 if zhandian in d[line[0]]['day']['qidian']:138 d[line[0]]['day']['qidian'][zhandian]+=1139 else :140 d[line[0]]['day']['qidian'][zhandian]=1141 142 #å¤ææé«å³°èµ·ç¹ï¼å¹¶è®°å½æå¯è½çç»ç¹143 elif fzj >=1050 and fzj <=1170: 144 if yy==0 :145 #yy=1146 if zhandian in d[line[0]]['night']['qidian']:147 d[line[0]]['night']['qidian'][zhandian]+=1148 else :149 d[line[0]]['night']['qidian'][zhandian]=1150 151 152 if riqi>rizi:153 rizi=riqi154 #å°ä¸ä¸å¤©çæ©ææ¶é´æ®µçç»ç¹å½å
¥155 if z==0:156 z=1157 if day_zd in d[lastID]['day']['zhongdian']:158 d[lastID]['day']['zhongdian'][day_zd]+=1159 else:160 d[lastID]['day']['zhongdian'][day_zd]=1161 if zz==0:162 zz=1163 if night_zd in d[lastID]['night']['zhongdian']:164 d[lastID]['night']['zhongdian'][night_zd]+=1165 else:166 d[lastID]['night']['zhongdian'][night_zd]=1167 168 #å¤ææ©é«å³°èµ·ç¹ï¼å¹¶è®°å½æå¯è½çç»ç¹169 if fzj >=420 and fzj <=540 : 170 if y==0 :171 y=1172 if zhandian in d[line[0]]['day']['qidian']:173 d[line[0]]['day']['qidian'][zhandian]+=1174 else :175 d[line[0]]['day']['qidian'][zhandian]=1176 177 #å¤ææé«å³°èµ·ç¹ï¼å¹¶è®°å½æå¯è½çç»ç¹178 elif fzj >=1050 and fzj <=1170: 179 if yy==0 :180 #yy=1181 if zhandian in d[line[0]]['night']['qidian']:182 d[line[0]]['night']['qidian'][zhandian]+=1183 else :184 d[line[0]]['night']['qidian'][zhandian]=1185 186 d.pop('') 187 188 #å¤æ家ï¼å·¥ä½å°ç¹189 d_add={} #add=address190 li=['day_qd_max','day_zd_max','night_qd_max','night_zd_max']191 l_ri=['day','night']192 l_qz=['qidian','zhongdian']193 for k,v in d.items():194 d_add[k]={}195 for x in li:196 locals()[x]=''197 nn=0198 #ç»liéçåéèµå¼ï¼åå«è¡¨ç¤ºæ©æèµ·ç»ç¹çæ大å¼çç«ç¹199 for t in l_ri:200 for tt in l_qz:201 if v[t][tt]:202 locals()[li[nn]]=sorted(v[t][tt],key=lambda x:v[t][tt][x])[-1]203 else:204 locals()[li[nn]]='null'205 nn+=1206 """#å¦æ家åºå·¥ä½å°ç¹é½æ¯å°éç«207 if day_qd_max == night_zd_max:208 d_add[k]['jia']= day_qd_max209 if day_zd_max==night_qd_max:210 d_add[k]['gongsi']= day_zd_max"""211 212 #å¦æä¸æ¯å®¶æå
¬å¸æ没æå°éç«æBRTç«ï¼ç®åç认为ç½å¤©èµ·ç¹æå¤çæ¯å®¶ï¼æä¸èµ·ç¹æå¤çæ¯å·¥ä½å°ç¹213 if 'jia' not in d_add[k]:214 d_add[k]['jia']= day_qd_max215 if 'gongsi' not in d_add[k]:216 d_add[k]['gongsi']= night_qd_max217 """#ä¹å¯ä»¥ææ¯ä¾æ大çä¼°ç®å®¶æå°ç¹,以ä¸æ¯ç¤ºä¾218 if 'jia' not in d_add[k]:219 dq_he=0220 d_add[k]['jia']=[]221 for x in v['day']['qidian'].values():222 dq_he+=x223 for kk,vv in v['day']['qidian'].items():224 if vv/dq_he>0.25:225 d_add[k]['jia'].append(kk) """226 if d_add[k]['jia']!='null' and d_add[k]['gongsi']!='null':227 strr=k+','+d_add[k]['jia']+','228 for key,value in v['night']['qidian'].items():229 strr+=(key+',')230 231 strr+='\n'232 l_inforid.append(strr)233 234 d_od={}235 l_jg=['jia','gongsi']236 l_zd=['null']237 for k,v in d_add.items():238 od=(v['jia'],v['gongsi'])239 if od not in d_od:240 d_od[od]=0241 for x in od:242 if x not in l_zd:243 l_zd.append(x)244 d_od[od]+=1245 l_shuchu=[' ,']246 for x in l_zd:247 l_shuchu.append(x)248 l_shuchu.append(',')249 l_shuchu.append('\n')250 rzs=0 #人æ»æ°251 zdwbd=0 #æ©ææä¸æ252 wdzbd=0 #æææ©ä¸æ253 for x in l_zd:254 l_shuchu.append(x)255 l_shuchu.append(',')256 if x !='null':257 if (x,'null') in d_od:258 zdwbd+=d_od[(x,'null')] #ç»è®¡æ©ææä¸æ259 if ('null',x) in d_od:260 wdzbd+=d_od[('null',x)] #ç»è®¡æææ©ä¸æ261 for xx in l_zd:262 if (x,xx) in d_od:263 l_shuchu.append(d_od[(x,xx)])264 rzs+=d_od[(x,xx)]265 l_shuchu.append(',')266 else:267 l_shuchu.append('0')268 l_shuchu.append(',')269 270 l_shuchu.append('\n')271 272 273 274 if os.path.exists('E:\广å·å
¬äº¤\yjt\人_od\\'+str(mm)+str(dd)) == False:275 os.mkdir('E:\广å·å
¬äº¤\yjt\人_od\\'+str(mm)+str(dd)+'/')276 277 with open(r'E:\广å·å
¬äº¤\yjt\人_od\\'+str(mm)+str(dd)+'\\shuchu'+str(mm)+str(dd)+'.csv','w+',encoding='utf-8')as f:278 for x in l_shuchu:279 f.write(str(x))280 281 282 with open(r'E:\广å·å
¬äº¤\yjt\人_od\\'+str(mm)+str(dd)+'\\id_information.csv','w+',encoding='utf-8')as f:283 for x in l_inforid:284 f.write(str(x))285 286 gg=0287 l_xinxi=['人æ°:',rzs,'\né«å³°å
å
¬äº¤æ¢ä¹ç人æ°:']288 with open(r'E:\广å·å
¬äº¤\yjt\人_od\\'+str(mm)+str(dd)+'\\id_information.csv','r+',encoding='utf-8')as f:289 for line in f.readlines():290 line=line.split(',')291 if len(line)>4:292 gg+=1293 l_xinxi.append(gg)294 295 with open(r'E:\广å·å
¬äº¤\yjt\人_od\\'+str(mm)+str(dd)+'\\xinxi.txt','w+',encoding='utf-8')as f:296 for x in l_xinxi:297 f.write(str(x))298 f.write('\néé«å³°æ车人æ°:')299 f.write(str(d_od[('null','null')]))300 f.write('\næ©ææä¸æ:')301 f.write(str(zdwbd))302 f.write('\næææ©ä¸æ:')303 f.write(str(wdzbd))304 ...
fuel_injection_perfection.py
Source:fuel_injection_perfection.py
1#operations: 1. add 1, 2. remove 1, 3. divide with 2 (even)23def solution(n):4 d={"1":0,"2":1}5 while True:6 dict_update={}7 for num in d:8 if(num==n):9 return d[num]10 m=int(num)11 if(str(m+1) not in d.keys()):12 dict_update.update({str(m+1):d[num]+1})13 if(m-1>1 and str(m-1) not in d.keys()):14 dict_update.update({str(m-1):d[num]+1})15 if(str(2*m) not in d.keys()):16 dict_update.update({str(2*m):d[num]+1})17 d.update(dict_update)18 #print(d)1920def solution2(n):21 d1={"1":0,"2":1} #dict with all minimum costs22 d2={"2":1} #temporary dict only for nth step with numbers of cost n23 cost=124 while True:25 d_add={}26 cost+=127 for num in d2: #explore only in d228 m=int(num)29 for num2 in [str(m+1),str(m-1),str(2*m)]:30 if num2 not in d1.keys():31 d_add.update({num2:cost})32 33 if(n in d_add):34 return d_add[n]3536 d1.update(d_add)37 d2=d_add3839 #print(d1)40 #print(d2)4142def solution3(n):43 # Mark all the vertices as not visited44 visited = {"1":0,"2":1}4546 # Create a queue for BFS47 queue = ["2"]4849 # Mark the source node as 50 # visited and enqueue it5152 while True:5354 # Dequeue a vertex from 55 # queue and print it56 #print(queue)57 s = queue.pop(0)58 #print (s, end = " ")5960 # Get all adjacent vertices of the61 # dequeued vertex s. If a adjacent62 # has not been visited, then mark it63 # visited and enqueue it64 num=int(s)65 cost=visited[s]66 for i in [str(num-1),str(num+1),str(2*num)]:67 if i not in visited:68 if(i==n): return cost+169 queue.append(i)70 visited[i] = cost+17172def solution4(n):73 t=n74 b=bin(int(n))75 steps=076 while True:77 steps+=178 if(b[-1]=='0'):79 t=int(b,2)//280 elif(b==bin(3) or b[-2:]=='01'):81 t=int(b,2)-182 else:83 t=int(b,2)+184 #print(t)85 b=bin(t)86 if(t==1):87 return steps888990def solution5(n):91 temp=int(n)92 steps=093 while True:94 a=temp%495 steps+=196 if(a==0 or a==2):97 temp=temp//298 elif(temp==3 or a==1):99 temp-=1100 else:101 temp+=1102 #print(t)103 if(temp==1):104 return steps105
...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!