Re: [問題] 如何寫一個程式可以把dict印出如下的格式
def product(target):
targets = [list(map(j.__add__, map(str, target[j]))) for j in target]
indices = range(len(targets))
iters = list(map(iter, targets))
values = list(map(next, iters))
while True:
print(''.join(values))
for i in indices:
try:
values[i] = next(iters[i])
break
except StopIteration:
iters[i] = iter(targets[i])
values[i] = next(iters[i])
else:
break
※ 引述《kadodo (想辦法突破)》之銘言:
: 如果我的dict如下
: target = {'a': [1,2], 'b': [3,4,5], 'c': [6,7],...}
: 怎麼寫一個程式 不管我的target有多少elements, 就是印出以下內容
: 以我舉的為例子 順序怎樣沒差 重要的是要列出 2x3x2=12種情況(12行)
: a1b3c6
: a1b3c7
: a1b4c6
: a1b4c7
: a1b5c6
: a1b5c7
: a2b3c6
: a2b3c7
: a2b4c6
: a2b4c7
: a2b5c6
: a2b5c7
--
「……私も、…っ、
母様の娘に生まれて…
母様とこれまでともに過ごしてきて…
本当に、
幸せでございました…っ!!」
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.112.217.2
→
10/03 21:45, , 1F
10/03 21:45, 1F
→
10/03 21:47, , 2F
10/03 21:47, 2F
→
10/03 21:52, , 3F
10/03 21:52, 3F
用itertools的寫法
from itertools import product as p
t = {'a': [1,2], 'b': [3,4,5], 'c': [6,7]}
for i in map(''.join, p(*(map(j.__add__, map(str, t[j])) for j in t))):
print(i)
不知道速度誰比較快(?
※ 編輯: mikapauli 來自: 140.112.233.183 (05/21 13:46)
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 2 篇):
Python 近期熱門文章
PTT數位生活區 即時熱門文章