C RUBY-ON-RAILS MYSQL ASP.NET DEVELOPMENT RUBY .NET LINUX SQL-SERVER REGEX WINDOWS ALGORITHM ECLIPSE VISUAL-STUDIO STRING SVN PERFORMANCE APACHE-FLEX UNIT-TESTING SECURITY LINQ UNIX MATH EMAIL OOP LANGUAGE-AGNOSTIC VB6 MSBUILD

# Python: group list items by adjacent same value

By : Kevin Leutzinger
Date : November 21 2020, 09:01 AM
wish of those help Use groupby() from itertools built-in module. For example:
code :
``````from itertools import groupby
data = ['a','s','s','s','d','e','e','s','s','a','a']
output = [(key, len(list(group))) for key, group in groupby(data)]
print output
``````

Share :

## Python: Locate 3 adjacent list items and determine list index of first of them

By : Stephanie Siebuhr
Date : March 29 2020, 07:55 AM
I wish this help you There is more than one way to do this. One way would be to search the list for the index and subtract two:
code :
``````list[ (i for i, j in enumerate( list ) if j.startswith( "1" ) ).next() - 2 ]
``````
``````import re
re.search( "\d{5}(?= \d{5} 1\d{4} 2\d{4} 3\d{4})", line )
``````

## Calculate difference between adjacent items in a python list

By : user3453297
Date : March 29 2020, 07:55 AM
wish of those help I have a list of millions of numbers. I want to find out if the difference between each number in the ordered list is the same for the entire list. , The straight approach here is the best:
code :
``````x = s[1] - s[0]
for i in range(2, len(s)):
if s[i] - s[i-1] != x: break
else:
#do some work here...
``````

## Python group by adjacent items in a list with same attributes

By : Rohan Pawar
Date : March 29 2020, 07:55 AM
it helps some times Assuming a items are always in pairs, a simple approach would be as follows.
Look at the first item - if it's an a, use it and the next item as a pair. Otherwise, just use the single item. Then 'jump' forward by 1 or 2, as appropriate:
code :
``````A=[(721,'a'),(765,'a'),(421,'a'),(422,'a'),(106,'b'),(784,'a'),(201,'a'),(206,'b'),(207,'b')]

result = []
count = 0
while count <= len(A)-1:
if A[count][1] == 'a':
result.append([A[count], A[count+1]])
count += 2
else:
result.append([A[count]])
count += 1

print(result)
``````

## Group recursively adjacent tuples from a list in Python

By : user3008079
Date : March 29 2020, 07:55 AM
it helps some times a somewhat UnionFind approach, iterating all 1-distanced pairs and "Unifying" their groups:
code :
``````from itertools import groupby, product

def Manhattan(tuple1, tuple2):
return abs(tuple1[0] - tuple2[0]) + abs(tuple1[1] - tuple2[1])

a = [(1, 1), (3, 1), (4, 5), (8, 8), (4, 4), (8, 9), (2, 1)]

tuple_pairs_with_distance_1 = [sorted(pair) for pair in product(a, repeat=2) if Manhattan(*pair) == 1]

result_dict = {t: {t} for t in a}
for t1, t2 in tuple_pairs_with_distance_1:
# "Unify" these tuple's groups
result_dict[t1] |= result_dict[t2]
result_dict[t2] = result_dict[t1]

result = [[*next(g)] for k, g in groupby(sorted(result_dict.values(), key=id), id)]
print(result)
``````

## How to compare two adjacent items in the same list - Python

By : Grand Gouda
Date : March 29 2020, 07:55 AM
I hope this helps . You can use zip():