Guest User

Python: Equivalent of str.split() for a list of strings - ExecutionTimeTest

a guest
May 16th, 2022
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.19 KB | None | 0 0
  1. setup1="""
  2. s_lines = \
  3. ["Action1,A,200,5",
  4. "Phase1,B,100,1,2000",
  5. "Action1,C,300,5",
  6. "Phase2,B,100,1,500",
  7. "Action1,C,400,5",
  8. "Action2,C,10,5",
  9. "Action3,C,10,5"]
  10.  
  11. s_expected_output = \
  12.    [["Action1,A,200,5"],
  13.    ["Action1,C,300,5"],
  14.    ["Action1,C,400,5", "Action2,C,10,5", "Action3,C,10,5"]]
  15.  
  16. def split_listofstrings_bysubstring(s_list: list, substring: str, delim_type='contains') -> list:
  17.  
  18.    split_list=list()
  19.    split_list.append(list())
  20.  
  21.    if delim_type == 'contains':
  22.        for line in s_list:
  23.            if substring in line and len(split_list[-1])!=0: # current line is delimiter
  24.                split_list.append(list())  # instantiate new sublist
  25.                continue
  26.            split_list[-1].append(line)
  27.    
  28.    if delim_type == 'startswith':
  29.        for line in s_list:
  30.            if line.startswith(substring) and len(split_list[-1])!=0: # current line is delimiter
  31.                split_list.append(list())  # instantiate new sublist
  32.                continue
  33.            split_list[-1].append(line)
  34.        
  35.    elif delim_type == 'endswith':
  36.        for line in s_list:
  37.            if line.endswith(substring) and len(split_list[-1])!=0: # current line is delimiter
  38.                split_list.append(list())  # instantiate new sublist
  39.                continue
  40.            split_list[-1].append(line)
  41.  
  42.    else:
  43.        raise ValueError(f"Parameter passed to delim_type was not recognised. Passed: {delim_type}")
  44.  
  45.    return split_list
  46. """
  47.  
  48.  
  49. test1 = "split_listofstrings_bysubstring(s_lines, substring = 'Phase', delim_type='startswith')"
  50.  
  51. setup2 = """
  52. s_lines = \
  53. ["Action1,A,200,5",
  54. "Phase1,B,100,1,2000",
  55. "Action1,C,300,5",
  56. "Phase2,B,100,1,500",
  57. "Action1,C,400,5",
  58. "Action2,C,10,5",
  59. "Action3,C,10,5"]
  60.  
  61. s_expected_output = \
  62.    [["Action1,A,200,5"],
  63.    ["Action1,C,300,5"],
  64.    ["Action1,C,400,5", "Action2,C,10,5", "Action3,C,10,5"]]
  65.  
  66. from itertools import groupby
  67. class GroupbyHelper(object):
  68.  
  69.    def __init__(self, val):
  70.        self.val = val
  71.        self.i = 0
  72.  
  73.    def __call__(self, val):
  74.        self.i += (self.val in val)
  75.        return self.i
  76.  
  77. def split_listofstrings_bysubstring_groupby(s_list: list, substring: str) -> list:
  78.    return [list(g) for k, g in groupby(s_list, key=GroupbyHelper(substring))]
  79.    
  80. substring = 'Phase'"""
  81.  
  82. test2 = """
  83. s_lines_split = split_listofstrings_bysubstring_groupby(s_lines, substring)
  84. s_lines_split_clean = list()
  85. for line in s_lines_split:
  86.    if substring in line[0]:
  87.        s_lines_split_clean.append(line[1:])
  88.    else:
  89.        s_lines_split_clean.append(line)"""
  90.  
  91. import timeit
  92. # from prefixed import Float
  93.  
  94. loop = int(1E6)
  95.  
  96. result = timeit.timeit(test1, setup=setup1, number=loop)
  97. result_1 = result
  98. # print(f"{Float(result/ loop):.2h}s per loop")
  99.  
  100. result = timeit.timeit(test2, setup=setup2, number=loop)
  101. # print(f"{Float(result/ loop):.2h}s per loop")
  102. result_2 = result
  103.  
  104. result = timeit.timeit(test2, setup=setup2, number=loop)
  105. # print(f"{Float(result/ loop):.2h}s per loop")
  106. result_2 += result
  107.  
  108. result = timeit.timeit(test1, setup=setup1, number=loop)
  109. # print(f"{Float(result/ loop):.2h}s per loop")
  110. result_1 += result
  111.  
  112. print(f"{result_2/result_1:.2%}")
Advertisement
Add Comment
Please, Sign In to add comment