|
If there are duplicate keys in the first list that map to different values in the second list, like a 1-to-many relationship, but you need the values to be combined or added or something instead of updating, you can do this: i = iter(["a", "a", "b", "c", "b"]) j = iter([1,2,3,4,5]) k = list(zip(i, j)) for (x,y) in k: if x in d: d[x] = d[x] + y #or whatever your function needs to be to combine them else: d[x] = yIn that example, d == {'a': 3, 'c': 4, 'b': 8} (责任编辑:) |
