python 列表 合并 1

a = [1, 3, 4, 2, 5, 6]
b = [99, 199, 199, 299, 99, 99]

c = {}

for i in range(len(b)):
    print('***************************** ', b[i])


    if str(b[i]) not in list(c.keys()):
        c['%s' % b[i]] = [a[i]]

    elif str(b[i]) in list(c.keys()):
        c['%s' % b[i]].append(a[i])

print(c)   #     {'99': [1, 5, 6], '299': [2], '199': [3, 4]}

print('---------------------------------------------------------------------------------------------------------')
def aaa(a, b):
   dic = {}
   for i in range(0, len(b)):
      if b[i] in dic:
         dic[b[i]].append(str(a[i]))
      else:
         dic[b[i]] = list(str(a[i]))
   print(dic)
a = [1, 3, 4, 2, 5, 6]
b = [99, 199, 199, 299, 99, 99]

aaa(a, b)    #    {299: ['2'], 99: ['1', '5', '6'], 199: ['3', '4']}


猜你喜欢

转载自blog.csdn.net/l1159015838/article/details/82110676