Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Number of elements in Python Set

I have a list of phone numbers that have been dialed (nums_dialed). I also have a set of phone numbers which are the number in a client's office (client_nums) How do I efficiently figure out how many times I've called a particular client (total)

For example:

>>>nums_dialed=[1,2,2,3,3]
>>>client_nums=set([2,3])
>>>???
total=4

Problem is that I have a large-ish dataset: len(client_nums) ~ 10^5; and len(nums_dialed) ~10^3.

like image 379
Tim Avatar asked Mar 27 '10 07:03

Tim


People also ask

How do you find the number of elements in a set in Python?

To determine how many items a set has, use the len() method.

What is Len set ()) in Python?

So len(set(x)) tells you the size of the set of unique elements of x .

What does set () do in Python?

set() method is used to convert any of the iterable to sequence of iterable elements with distinct elements, commonly called Set. Parameters : Any iterable sequence like list, tuple or dictionary. Returns : An empty set if no element is passed.

How many set operations are there in Python?

Python set operations (union, intersection, difference and symmetric difference)


2 Answers

which client has 10^5 numbers in his office? Do you do work for an entire telephone company?

Anyway:

print sum(1 for num in nums_dialed if num in client_nums)

That will give you as fast as possible the number.


If you want to do it for multiple clients, using the same nums_dialed list, then you could cache the data on each number first:

nums_dialed_dict = collections.defaultdict(int)
for num in nums_dialed:
    nums_dialed_dict[num] += 1

Then just sum the ones on each client:

sum(nums_dialed_dict[num] for num in this_client_nums)

That would be a lot quicker than iterating over the entire list of numbers again for each client.

like image 184
nosklo Avatar answered Sep 26 '22 13:09

nosklo


>>> client_nums = set([2, 3])
>>> nums_dialed = [1, 2, 2, 3, 3]
>>> count = 0
>>> for num in nums_dialed:
...   if num in client_nums:
...     count += 1
... 
>>> count
4
>>> 

Should be quite efficient even for the large numbers you quote.

like image 45
Eli Bendersky Avatar answered Sep 24 '22 13:09

Eli Bendersky