如何在Python中使用循环时根据条件合并两个大列表

发布于 2025-01-16 04:32:53 字数 2000 浏览 0 评论 0原文

我有两个相当大的列表,分别命名为 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中的while、for和if等循环。

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 年只有一个夏季,随后所有年份都按顺序排列冬季。 我怎样才能得到理想的结果?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

拧巴小姐 2025-01-23 04:32:53

我认为,下面的代码可以解决您的问题。

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)

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)
_失温 2025-01-23 04:32:53

我将首先组合/合并列表,然后对它们进行排序。我认为同时进行这两项工作会更复杂

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

你曾走过我的故事 2025-01-23 04:32:53

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(索引从零开始)。

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).

合约呢 2025-01-23 04:32:53

不知道为什么这些列表包含字符串,这些列表本身显然是字符串化的列表。

在这种情况下,最简单的解决方案是

result = sorted(windS + cindS)

将两个列表连接成一个大列表,并按词法对结果进行排序。这似乎适用于上面的示例数据。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文