-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharrayflatenner.py
48 lines (42 loc) · 1.29 KB
/
arrayflatenner.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
'''
__author__= Tatiana Lapshina, __version__=1.1, 10/03/2021
This code below will flatten array(list or tuple) of numbers:
example: (1,18,(19,6,(100,32)))-->[1, 18, 19, 6, 100, 32]
final array will be a list
if the argument is number or string:
example: "hnksh"-->["hnksh"]
example: 999.14 -->[999.14]
KeyErrors when dictionary passed
'''
from threading import Thread
def flattenArray(element):
b=[]
b= flattenElement([],element)
return b
def flattenElement(partEl,element):
if hasattr(element,'__len__') and not isinstance(element, str):
for i in range(len(element)):
thread=Thread(target=flattenElement, args=[partEl,element[i]])
thread.start()
thread.join()
else:
partEl.append(element)
return partEl
def flattenAsString(element):
"""
Converts array to a string, removes all brackets and duplicate commas,
splits the string back into list
"""
newSt=str(element)
newSt=newSt.replace(" ","")
replacements = ["[","]","(",")"]
for rep in replacements:
newSt=newSt.replace(rep,",")
#can do better with regex:
while ",," in newSt:
newSt=newSt.replace(",,",",")
newSt=newSt.rsplit(",")
newSt.pop(0)
newSt.pop(-1)
newList = [int(elem) for elem in newSt]
return newList