python3 元组取出相同值作为字典的键

  orderTuple = (
       (77, '蚂蚁矿机T9 12T','1'),
       (77, '蚂蚁阿瓦隆5代矿机','2'),
       (77, '蚂蚁黑白矿机ST10','3'),
       (78, '蚂蚁矿机L3+ 504M','4'),
       (78, '蚂蚁黑白矿机ST10','5'),
       (79, '阿瓦隆5代矿机','6'),
       (79, '蚂蚁黑白矿机ST10','7'),
       (79, '蚂蚁阿瓦隆5代矿机','8')
   )

变为
   orderDict = {
       77: (('蚂蚁矿机T9 12T', '1'), ('蚂蚁阿瓦隆5代矿机', '2'), ('蚂蚁黑白矿机ST10', '3')),
       78: (('蚂蚁矿机L3+ 504M', '4'), ('蚂蚁黑白矿机ST10', '5')),
       79: (('阿瓦隆5代矿机', '6'), ('蚂蚁黑白矿机ST10', '7'), ('蚂蚁阿瓦隆5代矿机', '8'))
   }
orderRes = {}
for n in orderTuple:
   t = ((n[1],n[2]),)
   if n[0] not in orderRes:
       orderRes[n[0]] = t
   else:
       orderRes[n[0]] += t
发布了48 篇原创文章 · 获赞 0 · 访问量 320

猜你喜欢

转载自blog.csdn.net/qq_21389693/article/details/105734854
今日推荐