Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
90 views
in Technique[技术] by (71.8m points)

python - How to match multiple items from a list with one element from another list?

I need to match items from the list array (364 items) with the list months (12 items). There will be multiple items from array that correspond to each month in months.

array = ['309', '307', '303', '296', '322', '340', '321', '314', '327', '315', '316', '333', '296', '286', '289', '290', '316', '317', '333', '348', '398', '396', '404', '424', '402', '357', '320', '315', '321', '328', '312', '293', '302', '296', '286', '281', '281', '  0', '312', '326', '332', '293', '242', '259', '268', '316', '296', '303', '280', '308', '314', '298', '307', '303', '300', '284', '289', '337', '308', '300', '288', '333', '321', '373', '301', '272', '288', '322', '318', '314', '321', '297', '299', '306', '312', '325', '334', '390', '339', '317', '343', '336', '357', '366', '383', '379', '355', '342', '369', '362', '359', '360', '380', '388', '393', '362', '347', '335', '322', '334', '313', '309', '303', '304', '326', '354', '364', '360', '343', '335', '343', '366', '318', '347', '326', '327', '329', '334', '347', '364', '346', '338', '337', '325', '304', '285', '298', '310', '316', '311', '321', '323', '360', '351', '337', '345', '372', '367', '356', '331', '308', '323', '327', '312', '300', '296', '305', '323', '338', '328', '319', '315', '315', '313', '316', '314', '312', '354', '317', '323', '324', '352', '360', '342', '333', '348', '335', '320', '321', '326', '327', '323', '303', '318', '308', '307', '302', '293', '304', '322', '302', '307', '304', '302', '287', '290', '306', '299', '297', '284', '289', '287', '316', '292', '291', '307', '300', '318', '302', '309', '320', '296', '293', '291', '287', '293', '287', '296', '293', '297', '287', '296', '296', '290', '287', '290', '290', '302', '298', '301', '297', '302', '290', '297', '288', '288', '299', '319', '311', '300', '302', '305', '294', '293', '288', '288', '289', '291', '286', '282', '280', '287', '279', '294', '345', '344', '292', '317', '296', '287', '288', '285', '291', '300', '298', '288', '288', '287', '275', '282', '288', '270', '272', '283', '284', '284', '295', '290', '279', '290', '287', '276', '289', '286', '295', '301', '287', '332', '305', '304', '275', '263', '266', '256', '257', '269', '258', '257', '273', '291', '277', '272', '280', '266', '269', '256', '282', '274', '308', '295', '288', '331', '290', '295', '283', '288', '285', '267', '274', '279', '300', '290', '293', '308', '285', '288', '279', '270', '281', '297', '296', '275', '255', '242', '239', '242', '269', '275', '278', '286', '276', '269', '283', '290', '317', '286', '287', '282', '273', '289', '322', '352', '268', '290', '311', '277', '256', '246', '255', '252', '265', '269', '265', '278', '272', '273', '302', '287', '284', '316', '318', '310', '280', '288', '293', '291'] 

months=['January 2020', 'February 2020', 'March 2020', 'April 2020', 'May 2020', 'June 2020', 'July 2020', 'August 2020', 'September 2020', 'October 2020', 'November 2020', 'December 2020'] 

The 364 values in array correspond to the ozone data of each day from the last year 2020, therefore I need to match each 31 (or 30, even 29 in the case of February and December) items to the right month, considering that the first value in 'array' corresponds with the date 01-01-2020 and the last value corresponds with the 29-12-2020. I don't really know if there is a command or if I directly have to do it with code.

My final goal of the code is to plot, months on the x axis and array on the y axis, with any libraries like matplotlib:

import matplotlib.pyplot as plt 

plt.plot(months, array)
plt.ylabel('Ozone Madrid') 
plt.xlabel('Months') 
plt.show() 

Currently I cannot plot this because the arguments (the two lists) that take plt.plot() don't have the same number of items. That's why I thought on this previous step.

Are there any other ways to do this faster and more efficiently?

question from:https://stackoverflow.com/questions/65661769/how-to-match-multiple-items-from-a-list-with-one-element-from-another-list

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use monthrange function from calendar (built in) to get the number of days of a month. Your code would be something like this

from calendar import monthrange

month_vals = []

for i in range(1, 13):
    vals = array[:monthrange(2020, i)[1]]
    array = array[monthrange(2020, i)[1]:]
    month_vals.append(vals)

NOTE: Your array has 366 values not 364


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...