dictionary - Python - converting string key to integer -
i have dictionary in following order. dictionary dynamically generated using from collections import defaultdict
ref = defaultdict(<type 'list'>, {'344': [('0', '0', '136'), ('548', '136', '137'), ('548', '136', '6')], '345': [('0', '0', '136'), ('548', '136', '137'), ('548', '136', '6'), ('742', '136', '6')]} what tried:
from here
ref = {int(k):[int(i) in v] k,v in ref.items()} but error:
typeerror: int() argument must string or number, not 'tuple' what want:
i want convert keys , values in string integers
ref = defaultdict(<type 'list'>, {344: [(0, 0, 136), (548, 136, 137), (548, 136, 6)], 345: [(0, 0, 136), (548, 136, 137), (548, 136, 6), (742, 136, 6)]}
your values list , items tuple need convert tuples items int,you can use map function :
ref = {int(k):[map(int,i) in v] k,v in ref.items()} and if in python 3 can use generator expression within tuple :
ref = {int(k):[tuple(int(t) t in i) in v] k,v in ref.items()} also since map returns list if want result tuple can use recipe too.
Comments
Post a Comment