1
justfly 2016-05-19 14:47:52 +08:00
sorted([(key, i+1) for key, value in d.items() for i in range(value)])
|
2
wang9571 2016-05-19 14:47:59 +08:00 3
[(x, i) for x, y in d.items() for i in range(1, y + 1)]
|
3
chevalier OP 为啥不能 append 了???
想到了另外一种 L=reduce(lambda x,y: x+y, [[(a, i) for i in range(1, n+1)] for a, n in d.iteritems()]) |
5
felixzhu 2016-05-19 15:02:04 +08:00
[(x, i) for x, y in d.iteritems() for i in xrange(1, y + 1)]
|
6
holyzhou 2016-05-20 08:09:32 +08:00
应该二楼的兼容性高点
|
7
practicer 2016-06-18 22:27:19 +08:00
我的方案是这样:
from itertools import product l = [] for k in d: l += list(product(k, range(1,d[k]+1))) |
8
antian 2022-12-10 00:37:22 +08:00
python 的 Iterable
|
9
antian 2022-12-10 00:41:00 +08:00
d = {'x': 2, 'y': 3, 'z': 5}
ls = [] for i, j in d.items(): # j 是一个迭代器 for x in range(1, j + 1): ls.append((i, x)) print(ls) 分解下 |