max_depth = [[i]*36 for i in range(1,11)]
max_depth = [j for k in max_depth for j in k]
I want to convert the above code in one line.
Instead of creating a nested list with [i]*36
, add another for
to retrieve i
36
times:
[i for i in range(1,11) for _ in range(36)]
Which would be equivalent to:
max_depth = []
for i in range(11):
for _ in range(36):
max_depth.append(i)
Using the itertools
module:
from itertools import chain
max_depth = list(chain.from_iterable([i]*36 for i in range(1,11)))
Going one step further with itertools
,
from itertools import chain, repeat
max_depth = list(chain.from_iterable(repeat(i, 36) for i in range(1,11)))
最直接的解决方案:
[j for k in [[i]*36 for i in range(1,11)] for j in k]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.