How to generate month names as list in Python? [duplicate]

2024/11/15 6:55:30

I have tried using this but the output is not as desired

m = []
import calendar
for i in range(1, 13):m.append(calendar.month_name)
print(m)

Output: [<calendar._localized_month object at 0x7f901a7013d0>, <calendar._localized_month object at 0x7f901a7013d0>, <calendar._localized_month object at 0x7f901a7013d0>, <calendar._localized_month object at 0x7f901a7013d0>, <calendar._localized_month object at 0x7f901a7013d0>, <calendar._localized_month object at 0x7f901a7013d0>, <calendar._localized_month object at 0x7f901a7013d0>, <calendar._localized_month object at 0x7f901a7013d0>, <calendar._localized_month object at 0x7f901a7013d0>, <calendar._localized_month object at 0x7f901a7013d0>, <calendar._localized_month object at 0x7f901a7013d0>, <calendar._localized_month object at 0x7f901a7013d0>]

Answer

The month_name element acts like a list.

You can either subscript it:

>>> calendar.month_name[3]
'March'

Or use list on it:

>>> import calendar
>>> list(calendar.month_name)
['', 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']

Note the blank at index 0. There is no month zero...

Which leads to the other issue in your code. If you correct your code to be:

import calendar
m=[calendar.month_name[i] for i in range(1,13)]
# or
m=calendar.month_name[1:]

In either case you now have turned 'January' into element 0 instead of element 1. You will need to covert every date.

https://en.xdnf.cn/q/72011.html

Related Q&A

Getting ERROR: Double requirement given: setuptools error in zappa

I tried to deploy my Flask app with zappa==0.52.0, but I get an error as below;ERROR: Double requirement given: setuptools (already in setuptools==52.0.0.post20210125, name=setuptools) WARNING: You are…

PySpark - Create DataFrame from Numpy Matrix

I have a numpy matrix:arr = np.array([[2,3], [2,8], [2,3],[4,5]])I need to create a PySpark Dataframe from arr. I can not manually input the values because the length/values of arr will be changing dyn…

RunTimeError during one hot encoding

I have a dataset where class values go from -2 to 2 by 1 step (i.e., -2,-1,0,1,2) and where 9 identifies the unlabelled data. Using one hot encode self._one_hot_encode(labels)I get the following error:…

Is there a Mercurial or Git version control plugin for PyScripter? [closed]

Closed. This question is seeking recommendations for books, tools, software libraries, and more. It does not meet Stack Overflow guidelines. It is not currently accepting answers.We don’t allow questi…

How to make a color map with many unique colors in seaborn

I want to make a colormap with many (in the order of hundreds) unique colors. This code: custom_palette = sns.color_palette("Paired", 12) sns.palplot(custom_palette)returns a palplot with 12 …

Swap column values based on a condition in pandas

I would like to relocate columns by condition. In case country is Japan, I need to relocate last_name and first_name reverse.df = pd.DataFrame([[France,Kylian, Mbappe],[Japan,Hiroyuki, Tajima],[Japan,…

How to improve performance on a lambda function on a massive dataframe

I have a df with over hundreds of millions of rows.latitude longitude time VAL 0 -39.20000076293945312500 140.80000305175781250000 1…

How to detect if text is rotated 180 degrees or flipped upside down

I am working on a text recognition project. There is a chance the text is rotated 180 degrees. I have tried tesseract-ocr on terminal, but no luck. Is there any way to detect it and correct it? An exa…

Infinite loops using for in Python [duplicate]

This question already has answers here:Is there an expression for an infinite iterator?(7 answers)Closed 5 years ago.Why does this not create an infinite loop? a=5 for i in range(1,a):print(i)a=a+1or…

How to print the percentage of zipping a file python

I would like to get the percentage a file is at while zipping it. For instance it will print 1%, 2%, 3%, etc. I have no idea on where to start. How would I go about doing this right now I just have the…