python - Replicate identical tuples to N -
this question has answer here:
i have identical tuples of (0, 1) assigned define limits 3 input values:
bounds = ((0, 1), (0, 1), (0, 1))
is there pythonic way assign same tuples n inputs? example:
bounds = ((0, 1), (0, 1), (0, 1), (0, 1), (0, 1), ...nth(0, 1))
you can multiply sequence n shallow copies of contents:
bounds = ((0, 1),) * n
this fine tuples of ints or other immutable data structures containing immutable types, cause surprising behavior if use mutable data structures lists - sequence of n references same list, because it's shallow copy. in case, comprehension idiomatic way create n independent objects:
mutable_bounds = [[0, 1] _ in range(n)]
Comments
Post a Comment