Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- setup1="""
- s_lines = \
- ["Action1,A,200,5",
- "Phase1,B,100,1,2000",
- "Action1,C,300,5",
- "Phase2,B,100,1,500",
- "Action1,C,400,5",
- "Action2,C,10,5",
- "Action3,C,10,5"]
- s_expected_output = \
- [["Action1,A,200,5"],
- ["Action1,C,300,5"],
- ["Action1,C,400,5", "Action2,C,10,5", "Action3,C,10,5"]]
- def split_listofstrings_bysubstring(s_list: list, substring: str, delim_type='contains') -> list:
- split_list=list()
- split_list.append(list())
- if delim_type == 'contains':
- for line in s_list:
- if substring in line and len(split_list[-1])!=0: # current line is delimiter
- split_list.append(list()) # instantiate new sublist
- continue
- split_list[-1].append(line)
- if delim_type == 'startswith':
- for line in s_list:
- if line.startswith(substring) and len(split_list[-1])!=0: # current line is delimiter
- split_list.append(list()) # instantiate new sublist
- continue
- split_list[-1].append(line)
- elif delim_type == 'endswith':
- for line in s_list:
- if line.endswith(substring) and len(split_list[-1])!=0: # current line is delimiter
- split_list.append(list()) # instantiate new sublist
- continue
- split_list[-1].append(line)
- else:
- raise ValueError(f"Parameter passed to delim_type was not recognised. Passed: {delim_type}")
- return split_list
- """
- test1 = "split_listofstrings_bysubstring(s_lines, substring = 'Phase', delim_type='startswith')"
- setup2 = """
- s_lines = \
- ["Action1,A,200,5",
- "Phase1,B,100,1,2000",
- "Action1,C,300,5",
- "Phase2,B,100,1,500",
- "Action1,C,400,5",
- "Action2,C,10,5",
- "Action3,C,10,5"]
- s_expected_output = \
- [["Action1,A,200,5"],
- ["Action1,C,300,5"],
- ["Action1,C,400,5", "Action2,C,10,5", "Action3,C,10,5"]]
- from itertools import groupby
- class GroupbyHelper(object):
- def __init__(self, val):
- self.val = val
- self.i = 0
- def __call__(self, val):
- self.i += (self.val in val)
- return self.i
- def split_listofstrings_bysubstring_groupby(s_list: list, substring: str) -> list:
- return [list(g) for k, g in groupby(s_list, key=GroupbyHelper(substring))]
- substring = 'Phase'"""
- test2 = """
- s_lines_split = split_listofstrings_bysubstring_groupby(s_lines, substring)
- s_lines_split_clean = list()
- for line in s_lines_split:
- if substring in line[0]:
- s_lines_split_clean.append(line[1:])
- else:
- s_lines_split_clean.append(line)"""
- import timeit
- # from prefixed import Float
- loop = int(1E6)
- result = timeit.timeit(test1, setup=setup1, number=loop)
- result_1 = result
- # print(f"{Float(result/ loop):.2h}s per loop")
- result = timeit.timeit(test2, setup=setup2, number=loop)
- # print(f"{Float(result/ loop):.2h}s per loop")
- result_2 = result
- result = timeit.timeit(test2, setup=setup2, number=loop)
- # print(f"{Float(result/ loop):.2h}s per loop")
- result_2 += result
- result = timeit.timeit(test1, setup=setup1, number=loop)
- # print(f"{Float(result/ loop):.2h}s per loop")
- result_1 += result
- print(f"{result_2/result_1:.2%}")
Advertisement
Add Comment
Please, Sign In to add comment