実現したいこと
以下文章内でmin(),max()が想定しているような形で動いていないので解決策をアドバイスしてほしい
発生している問題・エラーメッセージ
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-123-d1ead93c94cf> in <module> 3 test = [dt.strptime(i.strip().lower() , "%Y-%m-%d %H:%M:%S" ).month for i in test.split(",")] 4 print(test) ----> 5 max_month, min_month = [max(test),min(test)] 6 x = ",\n".join([ f"IF(cp{i} = 1, 1, 0) AS cp{i}" for i in range(min_month , max_month + 1)]) TypeError: 'int' object is not callable
該当のソースコード
python
1from datetime import datetime as dt 2test = "2022-02-02 00:00:00,2022-03-01 00:00:00,2022-01-01 00:00:00" 3test = [dt.strptime(i.strip().lower() , "%Y-%m-%d %H:%M:%S" ).month for i in test.split(",")] 4max_month,min_month = [max(test),min(test)] 5x = ",\n".join([ f"IF(cp{i} = 1, 1, 0) AS cp{i}" for i in range(min_month , max_month + 1)]) 6print(x)
補足情報(FW/ツールのバージョンなど)
Google Colab
リストの内包表記のところでdatetimeのmonthを抜いているのでリスト内の型が想定していないようなデータ型をしている?と思い、以下のコードで確認しましたが、すべてintとして入っているため問題ないと結論づけました。
python
1test = "2022-02-02 00:00:00,2022-03-01 00:00:00,2022-01-01 00:00:00" 2test = [dt.strptime(i.strip().lower() , "%Y-%m-%d %H:%M:%S" ).month for i in test.split(",")] 3for i in test: 4 print(type(i))
以下出力
<class 'int'> <class 'int'> <class 'int'>
(ソース:https://dot-blog.jp/news/python-max-function/)このページ内にある以下のようなコードをColab内で実行しても同様のエラーが返ってくる
x = [30, 20, 10, 40] print(max(x))
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-139-465bd6f6712c> in <module> 1 x = [30, 20, 10, 40] ----> 2 print(max(x)) TypeError: 'int' object is not callable
> TypeError: 'int' object is not callable
max か min, もしくは両方をユーザ定義変数として使用しているのだと思います。例えば、max = 1 や min = 0 などという定義をどこかでしていないでしょうか?

回答2件
あなたの回答
tips
プレビュー