pythonf-文字列と辞書リテラル[重複]
f-string中括弧内で辞書リテラルを実行するにはどうすればよいですか?
d_all = {d:d+2 for d in range(10)}
keys = [2,5]
tmp_dict = {d:d_all[d] for d in keys}
print(f'values: {tmp_dict}')
print(f'keys: {[d for d in keys]}') # list works
print(f'dict: { {{d:d_all[d] for d in keys}} }' # ???
回答
2 AWhiteFox
これを試して:
print(f'dict: { {d:d_all[d] for d in keys} }')