weixin_46025415
weixin_46025415
采纳率72.7%
2020-08-12 22:37

列表整合问题该怎么办?

我想把list1变成list2这样,或者把list1变成dict1,请问该怎么办?

road = ['01','02','03','04','05','06']

list1 = [
['01', '04'], ['01', '02'], ['01', '03'], 
['02', '01'], ['02', '03'], ['02', '09'],['02', '08'], 
['03', '01'], ['03', '06'], ['03', '02'], ['03', '04'], 
['04', '01'], ['04', '05'], ['04', '03'], 
['05', '06'], ['05', '04'], ['05', '07'], 
['06', '05'], ['06', '07'], ['06', '08'], ['06', '03'], ['06', '02'],
]



list2 = [
['01', '04','02', '03'], 
['02', '01', '03', '09', '08'], 
['03', '01', '06', '02', '04'], 
['04', '01', '05, '03'], 
['05', '06, '04', '07'], 
['06', '05', '07', '08', '03', '02']
]


dict1= { 
'01':['02','03','04'],
'02':['01','03','06','08','09'],
'03':['01','02','04','06'],
'04':['01','03','05'],
'05':['04','06','07'],
 '06':['02','03','05','07','08'],
}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • qq_43656607 放风喽 9月前
    road = ['01','02','03','04','05','06']
    
    list1 = [
    ['01', '04'], ['01', '02'], ['01', '03'],
    ['02', '01'], ['02', '03'], ['02', '09'],['02', '08'],
    ['03', '01'], ['03', '06'], ['03', '02'], ['03', '04'],
    ['04', '01'], ['04', '05'], ['04', '03'],
    ['05', '06'], ['05', '04'], ['05', '07'],
    ['06', '05'], ['06', '07'], ['06', '08'], ['06', '03'], ['06', '02'],
    ]
    
    list2 = []
    for i in road:
        dict1 = {}
        dict1[i] = []
        for t in list1:
            if t[0] == i:
                dict1[i].append(t[1])
        list2.append(dict1)
    print(list2)
    
    
    点赞 1 评论 复制链接分享

相关推荐