Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I print values only when they appear more than once in a list in python

Tags:

python

list

I have the following list:

seqList = [0, 6, 1, 4, 4, 2, 4, 1, 7, 0, 4, 5]

I want to print the items in the list only when it is present more than once(in this case value 1 and 4) and I want to ignore the first value in the list (in this case value 0)

To count how many times each value is present in the list I have the following code:

from collections import Counter

seqList = [0, 6, 1, 4, 4, 2, 4, 1, 7, 0, 4, 6]

c = dict(Counter(seqList))
print(c)

with output:

{0: 2, 6: 1, 1: 2, 4: 4, 2: 1, 7: 1, 5: 1}

But I want to ignore everything but 1 and 4 And the first 0 in the list shouldn't count.

The output I want to print is:

-value 1 appears multiple times (2 times)
-value 4 appears multiple times (4 times)

Does anyone have an idea how I could achieve this?

like image 376
Nawin Narain Avatar asked Nov 26 '19 08:11

Nawin Narain


1 Answers

You could make the following adjustments:

c = Counter(seqList[1:])  # slice to ignore first value, Counter IS a dict already 

# Just output counts > 1
for k, v in c.items():
    if v > 1:
        print('-value {} appears multiple times ({} times)'.format(k, v))

# output
-value 1 appears multiple times (2 times)
-value 4 appears multiple times (4 times)
like image 171
user2390182 Avatar answered Oct 08 '22 05:10

user2390182