简体   繁体   中英

Python merge sets partially

I have a set of tuples (set1), where each tuple is (somename1, somename2, Number). Where number indicates the times we've seen somename1, somename2. I want to merge it with a different set (set2) where somename1, somename2 might be in the set, and update Number accordingly. Right now, for obvious reasons, set1 & set2 returns an empty set. How can I update set1 efficiently?

Sample data = Set1 = {('soda','coca cola',5), ('chocolate','mars',13)}

You could use a Counter:

>>> from collections import Counter
>>> Set1 = Counter({('soda','coca cola'): 5})
>>> Set2 = Counter({('soda','coca cola'): 3, ('chocolate','mars'): 10})
>>> Set1 + Set2
Counter({('soda', 'coca cola'): 8, ('chocolate', 'mars'): 10})

If you don't want to add keys from Set2 if they are not in Set1, you can use dict comprehension:

>>> Set1 = {('soda','coca cola'): 5}
>>> Set2 = {('soda','coca cola'): 3, ('chocolate','mars'): 10}
>>> {k: Set1[k] + Set2.get(k, 0) for k in Set1}

Here's an example if you want to use sets for input, and are just interested in the resulting data in any format (looks like I'm using defaultdict a lot lately):

from itertools import chain
from collections import defaultdict

s1 = set((
    ('a', 'b', 1), ('c', 'd', 2)
))
s2 = set((
    ('a', 'b', 2), ('c', 'd', 3), ('e', 'f', 4)
))

s3 = defaultdict(int)

for name1, name2, count in chain(s1, s2):
    s3[name1, name2] += count

for (name1, name2), total_count in s3.items():
    print(name1, name2, total_count)

Prints:

c d 5
e f 4
a b 3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM