我有兩個相當大的串列,分別命名為 A 和 B,它們就像
['[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]']
和
['[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]']
我想按照日期順序合并這兩個串列。理想的結果是,
'[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.
首先,我考慮使用回圈,雖然是回圈,而不是來自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
但它有困擾的結果,就像2013年僅只有一個夏季的夏季,隨著所有冬季的所有冬季日期都在整個冬季的日期。我如何獲得理想的結果?
uj5u.com熱心網友回復:
我想,以下代碼將解決您的問題。
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)
uj5u.com熱心網友回復:
我將首先結合/合并串列,然后,我會對它們進行排序。同時做兩個都是復雜的,我想
uj5u.com熱心網友回復:
Python有標準庫函式合并兩個排序的串列:heapq.merge。你可以在你的情況下,像這樣使用它:
from heapq import merge
count = list(merge(windS, cindS, key=lambda x: x[1:9]))
請注意,list呼叫是必要的,因為merge回傳一個迭代器,您可能需要串列。
該部分key=lambda x: x[1:9]意味著: 與以下標準合并:串列中每個專案的字母 2 到 9(索引從零開始)。
uj5u.com熱心網友回復:
不知道為什么這些串列包含字串,這些字串本身顯然是字串化的串列。
在這種情況下,最簡單的解決方案是
result = sorted(windS cindS)
這會將兩個串列連接成一個大串列,并按詞法對結果進行排序。這似乎適用于上面的示例資料。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/449393.html
下一篇:有序對的陣列索引
