Assigning the % operand in python in an iteration -
this question has answer here:
if have array numbers = [1, 2, 3] , loop depend on these numbers , inside loop have , dictionary or object want name numbers in array numbers, how can use percent operand assign values?
so like
numbers = [1, 2, 3] number in numbers: dictionary_"" = {}
where "" number.
just using percent operand, dictionary_%g = {} %(number)
, not work. how can this? thanks.
you can't and/or shouldn't name variables dynamically. can create list or dictionary contains these 3 dictionaries. two-level data structure.
numbers = [1, 2, 3] dictionaries = {} number in numbers: dictionaries[number] = {}
or, more compactly:
dictionaries = {number: {} number in numbers}
Comments
Post a Comment