4

I'm new to programming in python so please bear over with my newbie question...

I have one initial list (list1) , which I have cleaned for duplicates and ended up with a list with only one of each value (list2):

list1 = [13, 19, 13, 2, 16, 6, 5, 19, 20, 21, 20, 13, 19, 13, 16],

list2 = [13, 19, 2, 16, 6, 5, 20, 21]

What I want is to count how many times each of the values in "list2" appears in "list1", but I can't figure out how to do that without getting it wrong.

The output I am looking for is something similar to this:

Number 13 is represented 1 times in list1. ........ Number 16 is represented 2 times in list1.

AH7
  • 39
  • 1
  • 3
  • This post might be a good place to start http://stackoverflow.com/questions/2600191/how-can-i-count-the-occurrences-of-a-list-item-in-python – Alexander Morley Sep 19 '16 at 18:26

6 Answers6

6
visited = []
for i in list2:
  if i not in visited:
    print "Number", i, "is presented", list1.count(i), "times in list1"
    visited.append(i)
lycuid
  • 2,555
  • 1
  • 18
  • 28
6

The easiest way is to use a counter:

from collections import Counter
list1 = [13, 19, 13, 2, 16, 6, 5, 19, 20, 21, 20, 13, 19, 13, 16]
c = Counter(list1)
print(c)

giving

Counter({2: 1, 5: 1, 6: 1, 13: 4, 16: 2, 19: 3, 20: 2, 21: 1})

So you can access the key-value-pairs of the counter representing the items and its occurrences using the same syntax used for acessing dicts:

for k, v in c.items():
    print('- Element {} has {} occurrences'.format(k, v))

giving:

- Element 16 has 2 occurrences
- Element 2 has 1 occurrences
- Element 19 has 3 occurrences
- Element 20 has 2 occurrences
- Element 5 has 1 occurrences
- Element 6 has 1 occurrences
- Element 13 has 4 occurrences
- Element 21 has 1 occurrences
albert
  • 8,027
  • 10
  • 48
  • 84
1

Simplest, easiest to understand, no-magic-approach is to create an object(associative array) and just count the numbers in list1:

list1 = [13, 19, 13, 2, 16, 6, 5, 19, 20, 21, 20, 13, 19, 13, 16]

frequency_list = {}

for l in list1:
    if l in frequency_list:
        frequency_list[l] += 1
    else:
        frequency_list[l] = 1

print(frequency_list)

prints out this:

{
    16: 2,
    2: 1,
    19: 3,
    20: 2,
    5: 1,
    6: 1,
    13: 4,
    21: 1
}

meaning 16 is there twice, 2 is there once...

Martin Gottweis
  • 2,721
  • 13
  • 27
  • A much more compact version of this is to have `for item in list1: frequency_list[item] = frequency_list.get(item, 0) + 1`. I don't know whether you consider that "magical" but I do actually prefer it for clean code. I guess it does take a little bit more understanding, so maybe just a matter of opinion :) – roganjosh Sep 19 '16 at 18:19
  • @roganjosh: nice. I just thought that a newbie question requires a newbie answer ;-) – Martin Gottweis Sep 19 '16 at 18:22
  • 1
    How interesting. I started using my suggestion after seeing it in a [vid by Hettinger](https://www.youtube.com/watch?v=OSGv2VnC0go) around 24:25 mark. I decided to test and your approach was consistently faster by a reasonable margin... – roganjosh Sep 19 '16 at 18:33
1

You don't need to remove duplicates. When you add to a dictionary, automatically, the duplicates will be considered as single values.

list1 = [13, 19, 13, 2, 16, 6, 5, 19, 20, 21, 20, 13, 19, 13, 16]
counts = {s:list1.count(s) for s in list1}
print counts

{2: 1, 5: 1, 6: 1, 13: 4, 16: 2, 19: 3, 20: 2, 21: 1}
picmate 涅
  • 3,951
  • 5
  • 43
  • 52
0

You can also use operator

>>> list1 = [13, 19, 13, 2, 16, 6, 5, 19, 20, 21, 20, 13, 19, 13, 16],
>>> list2 = [13, 19, 2, 16, 6, 5, 20, 21]

>>> import operator
>>> for s in list2:
...    print s, 'appearing in :',  operator.countOf(list1, s)
SuperNova
  • 25,512
  • 7
  • 93
  • 64
0

In technical terms, list is a "type" of "object". Python has a number of built in types like strings (str), integers (int), and a few others that can be easily found on google. The reason this is important is because each object type has its own "methods". You can think of these methods as functions that complete common programming tasks and make your life easier.

Counting the number of occurrences in a list is an example of a common programing task. We can accomplish it with the count() method. For example, to count the number of times 13 appears in list1:

count_13 = list1.count(13)

We can also use a for loop to iterate over the whole list:

for x in list2:
    print(list1.count(x)) #This is for python version 3 and up

or for python versions older than 3:

for x in list2:
    print list1.count(x)
user144153
  • 829
  • 1
  • 12
  • 28