'Python extract elements from list with occasional pattern into a tuple
I have a list that is has a pattern like this [float, string, float string...] but occasionally in the pattern it changes to [float, string, float, string, string, float string...]. What I want to do is to extract the elements of the list to a tuple in the format of (float, name, NoneType or str) to do something with it later. Here is a small example:
arr = [1150.1, 'James', 3323.1, 'Steve', 9323.1, 'John', 1233.1, 'Gary', 'criminal', 3293.1, 'Josh', 9232.1, 'Daniel', 'criminal']
I want to extract the list so the tuples look like this:
(1150.1, James, NONE)
(3323.1, Steve, NONE)
(9323.1, John, NONE)
(1233.1, Gary, criminal)
(3293.1, Josh, NONE)
(9232.1, Daniel, criminal)
so far i've tried checking for the next index in the array for the type but it's not working:
for index in range(len(arr)):
if type(arr[index]) == float and type(arr[index+1]) == str:
tup = arr[index], arr[index+1], None
print(tup)
elif type(arr[index]) == float and type(arr[index+1]) == str and type(arr[index+2]) == str:
tup = arr[index], arr[index + 1], arr[index+2]
print(tup)
Solution 1:[1]
You can keep track of the array elements you've seen since the last floating value using an auxiliary list. Whenever you see a float, turn the existing elements into a tuple and clear the auxiliary list:
result = []
items = []
for item in arr:
if isinstance(item, float) and items:
if len(items) < 3:
items.append(None)
result.append(tuple(items))
items = [item]
else:
items.append(item)
result.append(tuple(items))
print(result)
This outputs:
[
(1150.1, 'James', None), (3323.1, 'Steve', None),
(9323.1, 'John', None), (1233.1, 'Gary', 'criminal'),
(3293.1, 'Josh', None), (9232.1, 'Daniel', 'criminal')
]
Solution 2:[2]
You could check for the "float", "string" pattern and append accordingly:
output = list()
for i, element in enumerate(arr):
if isinstance(element, float) and isinstance(arr[i+1], str):
if isinstance(arr[i+2], str):
t = tuple(arr[i:i+3])
else:
t = tuple(arr[i:i+2]+["NONE"])
output.append(t)
>>> output
[(1150.1, 'James', 'NONE'),
(3323.1, 'Steve', 'NONE'),
(9323.1, 'John', 'NONE'),
(1233.1, 'Gary', 'criminal'),
(3293.1, 'Josh', 'NONE'),
(9232.1, 'Daniel', 'criminal')]
Solution 3:[3]
Another solution:
from itertools import groupby
g1 = (g for v, g in groupby(arr, type) if v is float)
g2 = (g for v, g in groupby(arr, type) if v is str)
out = [(next(a), *[*b, None][:2]) for a, b in zip(g1, g2)]
print(out)
Prints:
[
(1150.1, "James", None),
(3323.1, "Steve", None),
(9323.1, "John", None),
(1233.1, "Gary", "criminal"),
(3293.1, "Josh", None),
(9232.1, "Daniel", "criminal"),
]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | BrokenBenchmark |
| Solution 2 | not_speshal |
| Solution 3 | Andrej Kesely |
