6

Actually, I have a dataset about a "meeting". For example, A,B,C have a meeting, then the list would be [A,B,C]. Like this, each list would contain a list of members who participated in the meeting. Therefore:

line1= (A,B,C)

line2= (A,C,D,E)

line3 = (D,F,G)

...

I just would like to count the number how many times each pair of members meet each other. For example, member A meets C two times from line1 and line2 and member B meets C one time from line1. So, I would like to make a chart like this..

    A  B  C  D E F G...

 A  .  1  2  1 ...  

 B  1  . 1  0 

 C

...

I thought it would be easy at the first but I am pretty confused. Please help me and thank you so much in advance.

1

3 Answers 3

8

Rather than manually summing frequencies, use collections.counter along with itertools:

from collections import Counter
from itertools import chain, combinations

meets = Counter(chain.from_iterable(combinations(line, 2) for line in lines))

Where lines is an iterable of iterables of names.

3
  • +1 for using Python libraries. Everything's in there somewhere. >:P Commented Jun 1, 2012 at 5:20
  • +1 for a solution so blindingly self-apparent I'm kicking myself for not noticing it until your answer. When the other answer uses defaultdict(int) and does d[item] += 1 a lot, that's kind of screaming out for a Counter. Not to mention the question being "I want to count ...".
    – lvc
    Commented Jun 1, 2012 at 5:55
  • 1
    Note that this will only work if the elements are in the same order in every list, e.g. Counter(chain.from_iterable(combinations(x,2) for x in [[1,2],[2,1]])) produces Counter({(1, 2): 1, (2, 1): 1}). If you want to count each pairing regardless of order, turn each list into a set first: Counter(chain.from_iterable(combinations(x,2) for x in [set([1,2]),set([2,1])])) produces Counter({(1, 2): 2}).
    – Katrina
    Commented Apr 12, 2014 at 15:55
0

It looks like you should be able to solve this with matrix addition. If you know the total number of people (G in the question), then your answer is going to be a GxG matrix. Create a GxG matrix with the combinations from line1, then add in a GxG matrix with the combinations from line2, etc.

0

This is a pretty simple data-structure problem with a 2D array or dict. Arrays are more efficient if you have a lot of people, but I'll be assuming you don't.

times_met = defaultdict(int)
for line in lines:
     for pair in itertools.combinations(line, 2)
         times_met[pair] += 1

# How many times person a meets person b is described by the following (s.t. a < b)
print times_met[(a, b)]

Note that this is really inefficient if you have huge meetings and more efficient algorithms probably exist.

4
  • 1
    I think a dict of tuple -> int would make more sense - so that people_met[(person1, person2)] is the meetings between them. Then it doesn't need to be a defaultdict - just populate it initially from itertools.combinations.
    – lvc
    Commented Jun 1, 2012 at 5:03
  • @lvc A defaultdict(int) makes more sense semantically. If a new person joins the dataset, you can ask for how many times he's been in a meeting with anyone else and get the right answer - 0 - rather than a KeyError. Also initializing with zeros is pretty un-Pythonic. You never need a defaultdict but it allows you to write better code.
    – agf
    Commented Jun 1, 2012 at 5:20
  • The edit is good, but this is still inefficient for large datasets because you generate the cartesian product of the line with itself, instead of just the combinations. Remember that Python is batteries included -- there is already a way to do that.
    – agf
    Commented Jun 1, 2012 at 5:24
  • Yep, thanks for the tip; I kind of misunderstood the first comment as that it would be best to use it to replace the initialization of defaultdict. This is much better.
    – phsource
    Commented Jun 1, 2012 at 5:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.