开发者

How to extract every possible values of python Dict's values to list

开发者 https://www.devze.com 2022-12-13 20:42 出处:网络
DICTA={\'bw\':[\'BW\',\'VW\'],\'b\':[\'BB\',\'V开发者_运维百科V\'],\'a\':[\'AA\']} DICTB={\'yn\':[\'$YN\',\'$YNN\'],\'ye\':[\'$YE\',\'A$Y\'],\'y\':[\'Y$\']}
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(), []))
]
0

精彩评论

暂无评论...
验证码 换一张
取 消