how to count consecutive duplicates in a python list

Cameron Hiruta

I have a list as follows, consisting of only (-1)s and 1s:

list1=[-1,-1,1,1,1,-1,1]

I'm trying to append the number of consecutive duplicates into a list, e.g.:

count_dups=[2,3,1,1] 

I've tried creating a new list and using the zip function as the first step, but can't seem to go on because of the cut-off end-value

list2=list1[1:]
empty=[]
for x,y in zip(list1,list2):
    if x==y:
        empty.append(x)
    else:
        empty.append(0)
Karin

You can use itertools.groupby:

from itertools import groupby
list1 = [-1, -1, 1, 1, 1, -1, 1]
count_dups = [sum(1 for _ in group) for _, group in groupby(list1)]
print(count_dups)

Output:

[2, 3, 1, 1]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

C# count consecutive duplicates in List<string>

From Dev

python count duplicates in nexted list

From Dev

Python: Count and Remove duplicates in the list of list

From Dev

Remove consecutive duplicates from a 2D list , python?

From Dev

Remove consecutive duplicates from a 2D list , python?

From Dev

How do I remove consecutive duplicates from a list?

From Dev

How to count duplicates value in a list in reference to order

From Dev

How to get consecutive word count of a string python

From Dev

Count the duplicates in a list of tuples

From Dev

Removing consecutive duplicates from a list in Prolog

From Dev

Counting consecutive duplicates of strings from a list

From Dev

how can i count duplicates in a list and change the item value

From Dev

How to find duplicates values in list Python

From Dev

How to remove duplicates from a list python

From Dev

How to remove duplicates elements in the list in Python

From Dev

count number of duplicates in a list in OCaml

From Dev

Python - Count consecutive frequencies by group

From Dev

How to get count of consecutive dates

From Dev

how to find whether this python list within list contains duplicates or not?

From Dev

Eliminate Duplicates of a List (Python)

From Dev

Python: looking for duplicates in list

From Dev

not appending duplicates in list -python

From Dev

Collapse duplicates in python list

From Dev

Count of consecutive duplicate integers in a list with frequency

From Dev

Count multiple consecutive letters in a list in Scheme

From Dev

How to choose only consecutive numbers from a list using python

From Dev

How to choose only consecutive numbers from a list using python

From Dev

How to access non-consecutive slices of a list in Python

From Dev

Hey, i need to enter words to the list then count them an and print how many items in that list without counting duplicates

Related Related

  1. 1

    C# count consecutive duplicates in List<string>

  2. 2

    python count duplicates in nexted list

  3. 3

    Python: Count and Remove duplicates in the list of list

  4. 4

    Remove consecutive duplicates from a 2D list , python?

  5. 5

    Remove consecutive duplicates from a 2D list , python?

  6. 6

    How do I remove consecutive duplicates from a list?

  7. 7

    How to count duplicates value in a list in reference to order

  8. 8

    How to get consecutive word count of a string python

  9. 9

    Count the duplicates in a list of tuples

  10. 10

    Removing consecutive duplicates from a list in Prolog

  11. 11

    Counting consecutive duplicates of strings from a list

  12. 12

    how can i count duplicates in a list and change the item value

  13. 13

    How to find duplicates values in list Python

  14. 14

    How to remove duplicates from a list python

  15. 15

    How to remove duplicates elements in the list in Python

  16. 16

    count number of duplicates in a list in OCaml

  17. 17

    Python - Count consecutive frequencies by group

  18. 18

    How to get count of consecutive dates

  19. 19

    how to find whether this python list within list contains duplicates or not?

  20. 20

    Eliminate Duplicates of a List (Python)

  21. 21

    Python: looking for duplicates in list

  22. 22

    not appending duplicates in list -python

  23. 23

    Collapse duplicates in python list

  24. 24

    Count of consecutive duplicate integers in a list with frequency

  25. 25

    Count multiple consecutive letters in a list in Scheme

  26. 26

    How to choose only consecutive numbers from a list using python

  27. 27

    How to choose only consecutive numbers from a list using python

  28. 28

    How to access non-consecutive slices of a list in Python

  29. 29

    Hey, i need to enter words to the list then count them an and print how many items in that list without counting duplicates

HotTag

Archive