Home > Blockchain >  how to merge two big lists based on condition while using loop in python
how to merge two big lists based on condition while using loop in python

Time:03-23

I have two quite big lists which were named as A and B and they are like

['[20130602, 20130608]',
 '[20130609, 20130615]',
 '[20130630, 20130706]',
 '[20130804, 20130810]',
 '[20130901, 20130907]',
 '[20140601, 20140607]',
 '[20140720, 20140726]',
 '[20140727, 20140802]',
 '[20140803, 20140809]',
 '[20140810, 20140816]',
...
 '[20200719, 20200725]',
 '[20200726, 20200801]',
 '[20200802, 20200808]',
 '[20200809, 20200815]',
 '[20200816, 20200822]',
 '[20200823, 20200829]',
 '[20200830, 20200905]',
 '[20200906, 20200912]']

and

['[20131201, 20131207]',
 '[20131208, 20131214]',
 '[20131229, 20140104]',
 '[20140105, 20140111]',
 '[20140112, 20140118]',
 '[20140119, 20140125]',
 '[20141207, 20141213]',
 '[20141228, 20150103]',
 '[20150104, 20150110]',
...
 '[20210103, 20210109]',
 '[20210124, 20210130]',
 '[20210131, 20210206]',
 '[20210207, 20210213]',
 '[20210214, 20210220]',
 '[20210221, 20210227]']

I'd like to merge these two lists in order of the date. Desirable result is,

'[20130602, 20130608]',
'[20130609, 20130615]',
'[20130630, 20130706]',
'[20130804, 20130810]',
'[20130901, 20130907]', ---> summer in 2013
'[20131201, 20131207]',
'[20131208, 20131214]',
'[20131229, 20140104]',
'[20140105, 20140111]',
'[20140112, 20140118]',
'[20140119, 20140125]', ---> winter in the late of 2013 and in the early of 2014
'[20140601, 20140607]',
'[20140720, 20140726]',
'[20140727, 20140802]',
'[20140803, 20140809]', ... ] till the end of the last day in the early of 2021 from the second list. 

First of all, I considered using loop like while, for and if from python.

y = 13
while y < 22:
    for i in range(len(windS)):
        if windS[i][3:5] == str(y):
            count.append(windS[i])
    for j in range(len(cindS)):
        if cindS[j][3:5] == str(y):
            count.append(cindS[j])
        elif cindS[j][3:5] == str(y 1) and cindS[j][3:5] == '01' or '02' or '03':
            count.append(cindS[j])
            y  = 1

But it has bothering result like only have only one summer period in 2013 following by all winter dates in all years in order. How can I get the desirable result?

CodePudding user response:

I think, the below code will solve your problem.

date_list1 = ['[20130602, 20130608]',
 '[20130609, 20130615]',
 '[20130630, 20130706]',
 '[20130804, 20130810]',
 '[20130901, 20130907]',
 '[20140601, 20140607]',
 '[20140720, 20140726]',
 '[20140727, 20140802]',
 '[20140803, 20140809]',
 '[20140810, 20140816]',
 '[20200719, 20200725]',
 '[20200726, 20200801]',
 '[20200802, 20200808]',
 '[20200809, 20200815]',
 '[20200816, 20200822]',
 '[20200823, 20200829]',
 '[20200830, 20200905]',
 '[20200906, 20200912]']

date_list2 = ['[20131201, 20131207]',
 '[20131208, 20131214]',
 '[20131229, 20140104]',
 '[20140105, 20140111]',
 '[20140112, 20140118]',
 '[20140119, 20140125]',
 '[20141207, 20141213]',
 '[20141228, 20150103]',
 '[20150104, 20150110]',
 '[20210103, 20210109]',
 '[20210124, 20210130]',
 '[20210131, 20210206]',
 '[20210207, 20210213]',
 '[20210214, 20210220]',
 '[20210221, 20210227]']

len_date_list_1 = len(date_list1)
len_date_list_2 = len(date_list2)
  
res = []
i, j = 0, 0
  
while i < len_date_list_1 and j < len_date_list_2:
    if date_list1[i] < date_list2[j]:
      res.append(date_list1[i])
      i  = 1
  
    else:
      res.append(date_list2[j])
      j  = 1
  
res = res   date_list1[i:]   date_list2[j:]

print(res)

CodePudding user response:

I would begin by combining/merging the lists and after that, I would sort them. Doing both at the same time is more complicated I think

CodePudding user response:

Python has a function in the standard-library to merge two sorted lists: heapq.merge. You can use it in your case like this:

from heapq import merge

count = list(merge(windS, cindS, key=lambda x: x[1:9]))

Note that the list call is necessary because merge returns an iterator and you likely want a list.

The part key=lambda x: x[1:9] means: Merge with the following criteria: The letters two to nine of each item of the list (index is zero-based).

CodePudding user response:

No idea why these lists contain strings, which are apparently stringified lists themselves.

The most trivial solution in this case would be

result = sorted(windS   cindS)

This concatenates the two lists into a single big list and sorts the result lexically. Which appears to work on the sample data above.

  • Related