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 STRING) 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)
CodePudding user response:
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')
]
CodePudding user response:
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')]
CodePudding user response:
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"),
]
CodePudding user response:
Maybe you could use a while-loop:
from typing import Optional, NamedTuple
class Element(NamedTuple):
value: float
name: str
profession: Optional[str] = None
def main() -> None:
arr = [
1150.1, 'James',
3323.1, 'Steve',
9323.1, 'John',
1233.1, 'Gary', 'criminal',
3293.1, 'Josh',
9232.1, 'Daniel', 'criminal',
]
parsed_elements = []
i = 0
while i <= len(arr) - 2:
if isinstance(arr[i 2], str):
parsed_elements.append(Element(*arr[i:i 3]))
i = 3
else:
parsed_elements.append(Element(*arr[i:i 2]))
i = 2
print('\n'.join(str(e) for e in parsed_elements))
if __name__ == '__main__':
main()
Output:
Element(value=1150.1, name='James', profession=None)
Element(value=3323.1, name='Steve', profession=None)
Element(value=9323.1, name='John', profession=None)
Element(value=1233.1, name='Gary', profession='criminal')
Element(value=3293.1, name='Josh', profession=None)
Element(value=9232.1, name='Daniel', profession='criminal')