Linked Questions
25 questions linked to/from What's the most Pythonic way to identify consecutive duplicates in a list?
12
votes
3
answers
22k
views
how to count consecutive duplicates in a python list [duplicate]
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 ...
2
votes
4
answers
11k
views
Longest sequence of consecutive duplicates in a python list [duplicate]
As mentioned, a run is a sequence of consecutive repeated values. Implement a Python function called longest_run that takes a list of numbers and returns the length of the longest run. For example in ...
3
votes
4
answers
3k
views
How do I find the length of a run of numbers in a list? (Is there a faster way than what I'm doing?) [duplicate]
I have a list that contains either 1's or 0's; nothing else. I am interested in finding the 1's and, more specifically, where a run of 1's starts and where that run ends (or in the code below, the "...
2
votes
4
answers
2k
views
Grouping repetitions in an array? [duplicate]
I am looking for a function that gets a one dimensional sorted array and returns
a two dimensional array with two columns, first column containing non-repeated
items and second column containing ...
3
votes
1
answer
2k
views
Count consecutive elements in a list [duplicate]
I was doing a small program that count the number of times that a character appears in a list and I have a question, ¿Is possible to do it by consecutive numbers?.
I have this code now:
def function(...
-1
votes
1
answer
407
views
Replacing consecutive elements by the number of occurrences [duplicate]
I have a list of strings and numbers l = ['a','a',9,7,'b','c','c','c']
And the output wanted is ['a*2',9,7,'b','c*3']
This is what I have but it can only do '*2' and the last element is not affected ...
0
votes
2
answers
122
views
Count duplicated value in Python considering sequence [duplicate]
I have string value as:
s = 'asdabbdasfababbabb'
I've split the str by using below code, than get result as below :
n = 3
split_strings = [s[index : index + n] for index in range(0, len(s), n)]
['...
0
votes
3
answers
74
views
Remove multple occurance of a word with another from python list [duplicate]
I have a list in a particular format as follows:
my_list = ['apple', 'apple', 'boy', 'cat', 'cat', 'apple', 'apple',
'apple', 'boy', 'cat', 'cat', 'dog', 'dog'].
And my expected output ...
183
votes
9
answers
247k
views
Python group by
Assume that I have a set of data pair where index 0 is the value and index 1 is the type:
input = [
('11013331', 'KAT'),
('9085267', 'NOT'),
('5238761', 'ETH'),
...
94
votes
10
answers
78k
views
Is there any built-in way to get the length of an iterable in python? [duplicate]
For example, files, in Python, are iterable - they iterate over the lines in the file. I want to count the number of lines.
One quick way is to do this:
lines = len(list(open(fname)))
However, this ...
2
votes
8
answers
15k
views
Validate card numbers using regex python
I have some credit card numbers with me and want to validate them over the below rules.
► It must only consist of digits (0-9)
► It may have digits in groups of 4, separated by one hyphen "-"
► It ...
7
votes
4
answers
5k
views
How to get the index and occurance of each item using itertools.groupby()
Here's the story I have two lists:
list_one=[1,2,9,9,9,3,4,9,9,9,9,2]
list_two=["A","B","C","D","A","E","F","G","H","Word1","Word2"]
I want to find the indicies of consecutive 9's in list_one so that ...
7
votes
1
answer
814
views
find stretches of Trues in numpy array
Is there a good way to find stretches of Trues in a numpy boolean array? If I have an array like:
x = numpy.array([True,True,False,True,True,False,False])
Can I get an array of indices like:
starts =...
-2
votes
6
answers
4k
views
How to check if two consecutive digits are the same in python
I found this practice today:
Detect whether a check number is valid or not. The check number must have 10 digits, can not have 2 or more zeros followed, nor three or more digits other than zero ...
5
votes
6
answers
140
views
A pythonic way to delete successive duplicates of only one element in a list
I know there are many other similar questions posted, but there is a difference in mine that makes it unsolvable with their answers.
I have several lists of characters that may have multiple ...