Reputation: 25
I've my month_list that I got using most_common() from collection module that looks like this:
[('October', 3744), ('September', 3329), ('November', 3072), ('March', 2630), ('February', 2268), ('June', 2046), ('December', 1959), ('August', 1880), ('May', 1567), ('July', 1289), ('April', 1237), ('January', 1155)]
What I want is index the list with the correct months order, I've tried like this:
month_index = ['January', 'February',' March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
month_list.index(month_index)
But I get this error
ValueError: ['January', 'February', ' March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'] is not in list
Also I've another list month_list2 where January is totally missing, but when sorting I'd like to have something like this:
[('January','NA'), ('February', 568), ('March', 3481)...]
is it possible? Thanks!
Upvotes: 2
Views: 236
Reputation: 24059
If you have a duplicate month and you want sort it. you need to consider the second number in the tuple. I hope my answer help you. (in this example we have two 'January' and two 'February')
month_list = [('January', 3329), ('November', 3072), ('February', 2630), ('February', 2268), ('June', 2046), ('December', 1959), ('August', 1880), ('May', 1567), ('July', 1289), ('April', 1237), ('January', 1155)]
month_index = ['January', 'February','March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
month_list = month_list + [(m, 'NA') for m in month_index if m not in dict(month_list)]
sorted(month_list, key=lambda x: (month_index.index(x[0]), x[1]))
Output:
[('January', 1155),
('January', 3329),
('February', 2268),
('February', 2630),
('March', 'NA'),
('April', 1237),
('May', 1567),
('June', 2046),
('July', 1289),
('August', 1880),
('September', 'NA'),
('October', 'NA'),
('November', 3072),
('December', 1959)]
Upvotes: 1
Reputation: 79
You cannot pass a list to month_list.index() it accepts only one value as argument.
Moreover your month_list ist a list of tuples and you are trying to index a string in the list.
convert the month_list to a list of only months:
month_list_str = [i[0] for i in month_list]
Output:
month_list_str
Out[25]:
['October',
'September',
'November',
'March',
'February',
'June',
'December',
'August',
'May',
'July',
'April',
'January']
Then you can index and sort this list:
out_list = []
for m in month_index:
if m in month_list_str:
i = month_list_str.index(m)
out_list += [(m,month_list[i][1])]
else:
out_list += [(m,'NA')]
Output:
out_list
Out[39]:
[('January', 1155),
('February', 2268),
(' March', 'NA'),
('April', 1237),
('May', 1567),
('June', 2046),
('July', 1289),
('August', 1880),
('September', 3329),
('October', 3744),
('November', 3072),
('December', 1959)]
Upvotes: 1
Reputation: 71610
Try:
>>> dct = {'January': 1, 'February': 2, 'March': 3, 'April': 4, 'May': 5, 'June': 6, 'July': 7, 'August': 8, 'September': 9, 'October': 10, 'November': 11, 'December': 12}
>>> lot = [('October', 3744), ('September', 3329), ('November', 3072), ('March', 2630), ('February', 2268), ('June', 2046), ('December', 1959), ('August', 1880), ('May', 1567), ('July', 1289), ('April', 1237)]
>>> sorted([(k, dict(lot).get(k, 'NA')) for k in dct], key=lambda x: dct[x[0]])
[('January', 'NA'), ('February', 2268), ('March', 2630), ('April', 1237), ('May', 1567), ('June', 2046), ('July', 1289), ('August', 1880), ('September', 3329), ('October', 3744), ('November', 3072), ('December', 1959)]
>>>
Upvotes: 1
Reputation: 331
Here is a solution with inserting missing data
my_months = [
("October", 3744),
("September", 3329),
("November", 3072),
("March", 2630),
("February", 2268),
("June", 2046),
("December", 1959),
("August", 1880),
("May", 1567),
("July", 1289),
("April", 1237),
("January", 1155),
]
month_index = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
]
ordered_months = sorted(my_months, key=lambda v: month_index.index(v[0]))
# checks if all months exist
months_in_ordered_list = [month[0] for month in ordered_months]
for month in month_index:
if month not in months_in_ordered_list:
ordered_months.insert(month_index.index(month), (month, 'NA'))
Upvotes: 1
Reputation: 444
To sort list, you should use sort with index being its key:
month_list.sort(key = lambda item:month_index.index(item[0]))
.. but, you also want to add missing items. So you'd better convert month_list to a dict, and access dict item with NA being its default value:
month_dict = dict(month_list)
month_list = [(month, month_dict.get(month, 'NA')) for month in month_index]
Upvotes: 5
Reputation: 1053
You could do it using the module datetime
. The %B
represents the months' format.
The following can be achieved as shown below:
from datetime import datetime
months = [('October', 3744), ('September', 3329), ('November', 3072), ('March', 2630), ('February', 2268), ('June', 2046), ('December', 1959), ('August', 1880), ('May', 1567), ('July', 1289), ('April', 1237), ('January', 1155)]
sorted(months, key=lambda m: datetime.strptime(m[0], "%B"))
Upvotes: 2