SHARE
TWEET

find values from two datasets on same date

a guest Sep 3rd, 2012 37 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ######################
  2. #
  3. # function (2Darray1,2Darray2)
  4. # 2Darray1 and 2Darray2 are combined date and value as string.
  5. # date format can be edited in additional function.
  6. # both input arrays are in format [date:value,date:value,...,date:value]
  7. # example:
  8. # correctData([['2003.05.19', 26.0], ['2003.05.21', 25.0], ['2003.05.22', 32.0]],[['2003.05.21', 27.0], ['2003.05.22', 36.0],['2003.05.24', 25.0], ['2003.05.27', 32.0]])
  9. #
  10. # returns 2Darray without dates, only values which were found in same dates on both input arrays
  11. # return format [value1:value2:value3:...valueN, value1:value2:value3...valueN]
  12. # return example from previous input [[25 (from date 2003.05.21);32 (from date 2003.05.22)];[27 (from date 2003.05.21);36 (from date 2003.05.22)]]
  13. #
  14. ######################
  15. def correctData(garaisMasivs, isaisMasivs):
  16.     dataArray = []
  17.     saraksts = list()
  18.     xi=0                                        #==========iisaa masiiva counteris
  19.     yi=0                                        #==========garaa masiiva counteris
  20.     yc=0                                        #==========garaa masiiva countera counteris :D
  21.     garaMasivaGarums=len(garaisMasivs)-1        #======miinus viens, jo masiivaa pirmais ir nultais elements
  22.     isaMasivaGarums=len(isaisMasivs)-1
  23.     tmpArray=[[]*2 for x in xrange(2)]          #========definee 2d masiivu
  24.     while xi<=isaMasivaGarums:                  #========divi while cikli viens ieksh otra. flowcontrols taads, ka tikai vienu reizi iziet cauri
  25.                                                 #========masiivam atrodot vai nu elements ir "agraak", "veelaak", vai sakriit.
  26.                 yi=yc
  27.                 while yi<=garaMasivaGarums:     #==========cikls, kursh skataas, vai visi garaa masiiva elementi apskatiiti; to  pasaka yi (gara masiiva) counteris                        
  28.                                 if get_date(isaisMasivs[xi][0])<get_date(garaisMasivs[yi][0]):              
  29.                                                 #print "agraaks", xi, yi, garaisDatums, isaisDatums, garaisMasivs[xi-1][1]
  30.                                                 yi=len(garaisMasivs) #======break aizstaajeejs, ja ir lielaaks, tad pa taisno iet no garaa masiiva cikla aaraa                                              
  31.                                 elif get_date(isaisMasivs[xi][0])>get_date(garaisMasivs[yi][0]):
  32.                                                 yi+=1           #=========naakoshais garaa masiiva elements svariigs.
  33.                                                 yc=yi           #=========NO kura elementa turpinaat iisajaa masiivaaa!        
  34.                                 else:      
  35.                                                 tmpArray[0].append(garaisMasivs[yi][1])
  36.                                                 tmpArray[1].append(isaisMasivs[xi][1])
  37.                                                 break  
  38.                 xi=xi+1
  39.     return tmpArray
  40.  
  41.  
  42. #additional function get_date
  43. def get_date(record):
  44.                 return datetime.strptime(record, '%Y.%m.%d')
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top