次のタグが付いている新しい回答: python3
3
票
Accepted
utf-8に変換できないshift-jisの文字(環境依存文字)を、個別に変換する方法
質問の内容は、encodingをcp932にすれば解決しますが、「utf-8に変換できないshift-jisの文字(環境依存文字)を、個別に変換する方法」(質問文の例のように「(1)」にするなど)としては、codecs.register_errorで自前のエラーハンドラを登録して処理する方法もあります。
Python標準ライブラリ - codecs
[python] やむを得ない事情で utf-...
2
票
Accepted
Django REST API の返送で、json.dumps関数で生成した文字列が意図通りにならない
(質問が書かれてないので感想です)
https://docs.djangoproject.com/en/4.2/ref/request-response/#jsonresponse-objects で
return JsonResponse({'version':'ver 1.2'}, json_dumps_params={'indent': 2})
と返すべきでしょう
1
票
Accepted
Django データベースで外部参照をつなげる方法
自己解決したので回答します。
Djangoでは、InnerJoinをデフォルトで発行しているようです。
上記の例では、gender="男性"で絞り込みたい場合には、参照側を使って、
parson = ParsonInfo.objects.filter(gender="男性").all()
for obj in parson:
...
0
票
キーがユニークでないDataFrameをunstackする方法
df = df.T.groupby(df.index, axis=1).sum().T
df.index = pd.MultiIndex.from_tuples(df.index)
df = df.unstack(level=0)
df = df.swaplevel(axis='columns').sort_index(axis='columns')
# 埼玉 ...
0
票
DjangoのDBで、モデルクラスを利用することなく、実行したSQLを取得する方法
QuerySet.query を文字列化することでSQLが得られます(A)。
ログ出力という観点では、Django標準の機能を使う方法もあります(B)。
A. ORMのQuerySetオブジェクトを文字列化する
qs = User.objects.all()
print(str(qs.query))
A2. Aの文字列を読みやすくフォーマットする
qs = User.objects.all()
...
0
票
pythonで任意の要素数の組からなるリストを作りたい
https://docs.python.org/ja/3/library/itertools.html#itertools.product
itertools.productのrepeat引数がまさにそのための機能だと思います。
from itertools import product
for x in product(range(3), repeat=4):
print(x)
...
0
票
pythonで任意の要素数の組からなるリストを作りたい
More Itertools の chunked を使う場合。
from itertools import product
from more_itertools import chunked
from pprint import pprint
def cartesian_product(n):
return [*product(*chunked(range(3*n), 3))]
if ...
0
票
pythonで任意の要素数の組からなるリストを作りたい
NumPy での場合 (すべて NumPyというわけではないが)
from itertools import product
import numpy as np
N = 3
p = np.split(np.arange(3 *N), N)
list(product(*p))
#[(0, 3, 6),
# (0, 3, 7),
# (0, 3, 8),
# (0, 4, 6),
# (0, 4, ...
0
票
Accepted
pythonで任意の要素数の組からなるリストを作りたい
以下のような感じでよいと思います。
指定の要素数に見合った各要素の値の候補を用意して、それらをitertoolsのproductで全組み合わせ列挙します。
from itertools import product
N = 2
a = [list(map(lambda v: v+i*3, range(3))) for i in range(N)]
print(a) # [[0, 1, 2], [...
0
票
Pythonのオーバーライドしたメソッドに印をつける方法
【参考】
クラスの中に、クラスを定義し、継承関係がある場合でも、動作するようにしました。
(以前の回答例では、ネストしたクラスが含まれていると動作しませんでした。)
from typing import Any
from typing import Callable
import functools
import inspect
def get_attribute(obj, ...
上位 50 件の最近の回答が含まれています
関連するタグ
python3 × 1518python × 1300
pandas × 92
numpy × 63
matplotlib × 53
django × 50
windows × 35
web-scraping × 33
jupyter-notebook × 33
tkinter × 32
anaconda × 31
selenium × 31
アルゴリズム × 29
tensorflow × 27
beautifulsoup × 27
pyqt5 × 26
csv × 25
機械学習 × 24
macos × 22
json × 20
正規表現 × 20
opencv × 20
flask × 20
vscode × 19
ubuntu × 16