基于给定变量创建动态键:构建Bitbucket分支子分支映射字典
分支与子分支的字典映射实现
给定两个列表,其中listOfRepos存储分支路径,listOfChildBranches存储对应分支的子分支列表,需要生成以分支路径为键、子分支列表为值的字典,并输出指定格式的结果。
给定变量
listOfRepos = ['repo/association/branch/master','repo/publish/branch/master'] listOfChildBranches = [["childBranch1","childBranch2","childBranch3"], ["working1","demo2","rebased3"]]
实现代码
利用Python的zip()函数将两个列表按索引配对,直接转换为字典即可满足需求:
# 生成目标字典 branch_map = dict(zip(listOfRepos, listOfChildBranches)) # 按期望格式输出结果 for branch_path, child_branches in branch_map.items(): print(f"{branch_path}={child_branches}")
输出结果
repo/association/branch/master=['childBranch1', 'childBranch2', 'childBranch3'] repo/publish/branch/master=['working1', 'demo2', 'rebased3']
内容的提问来源于stack exchange,提问作者JerryBurnard
相关产品推荐
相关产品推荐

