如何在Python中使用循环时根据条件合并两个大列表
我有两个相当大的列表,分别命名为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我将首先组合/合并列表,然后对它们进行排序。我认为同时进行这两项工作会更复杂
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
Python 标准库中有一个函数可以合并两个排序列表: <代码>heapq.merge。
您可以在您的情况下使用它,如下所示:
请注意,
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:
Note that the
list
call is necessary becausemerge
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).不知道为什么这些列表包含字符串,这些列表本身显然是字符串化的列表。
在这种情况下,最简单的解决方案是
将两个列表连接成一个大列表,并按词法对结果进行排序。这似乎适用于上面的示例数据。
No idea why these lists contain strings, which are apparently stringified lists themselves.
The most trivial solution in this case would be
This concatenates the two lists into a single big list and sorts the result lexically. Which appears to work on the sample data above.