LoginSignup
1
0

More than 5 years have passed since last update.

辞書のvalueを片っ端から変換するマン

Posted at

ex: 辞書の中で、valueのtypeがDecimalの場合のみintに変換する

from decimal import Decimal
before = {'a': Decimal(100), 'b':20, 'c':'cccc', 'd': Decimal(200)}
after = dict([(k, int(v) if type(v) is Decimal else v) for k,v in before.items()]) 
print(after)
# {'a': 100, 'd': 200, 'c': 'cccc', 'b': 20}

最初にkey,valueのタプルにしてから、変換して再度dictに戻している。
走査関数でもっとどうにかできないものか。

1
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0