DICTA={'bw':['BW','VW'],'b':['BB','V开发者_运维百科V'],'a':['AA']}
DICTB={'yn':['$YN','$YNN'],'ye':['$YE','A$Y'],'y':['Y$']}
How to extract every possible values of that 2 Dict to
["BWYN","VWYN","BBYN","VVYN","AAYN","BWYNN","VWYNN","BBYNN","VVYNN","AAYNN",
"BWYE","VWYE","BBYE","VVYE","AAYE","ABWY","AVWY","ABBY","AVVY","AAAY",
"YBW","YVW","YBB","YVV","YAA"]
PS: Order does not matter
PPS: Not homework, but like to know how it can be implemented in efficient way.
Many possible minor variants on the following fundamental theme:
print [y.replace('$', x)
for y in (v for y in DICTB.values() for v in y)
for x in (v for y in DICTA.values() for v in y)
]
I like to go with itertools
myself, but essentially the same as Alex's solution:
from itertools import product
[
y.replace('$', x)
for (x, y) in product(sum(DICTA.values(), []), sum(DICTB.values(), []))
]
精彩评论