My function needs two params(a,b) and if...break
, which seems redundant. Can it be made prettier?
def slice_list(lst,num):
"""
Given a list or Numpy array,
get consecutive tuples based on specific window.
"""
rst = []
for i,_ in enumerate(lst):
a = i
b = i + num
if len(lst[a:b])<num:
break
rst.append(tuple(lst[a:b]))
return rst
Example:
>>> lst = np.array([10, 11, 12, 13, 26, 28])
>>> slice_list(lst,4)
[(10, 11, 12, 13), (11, 12, 13, 26), (12, 13, 26, 28)]
My function needs two params(a,b) and if...break
, which seems redundant. Can it be made prettier?
def slice_list(lst,num):
"""
Given a list or Numpy array,
get consecutive tuples based on specific window.
"""
rst = []
for i,_ in enumerate(lst):
a = i
b = i + num
if len(lst[a:b])<num:
break
rst.append(tuple(lst[a:b]))
return rst
Example:
>>> lst = np.array([10, 11, 12, 13, 26, 28])
>>> slice_list(lst,4)
[(10, 11, 12, 13), (11, 12, 13, 26), (12, 13, 26, 28)]
My function needs two params(a,b) and if...break
, which seems redundant. Can it be made prettier?
def slice_list(lst,num):
"""
Given a list or Numpy array,
get consecutive tuples based on specific window.
"""
rst = []
for i,_ in enumerate(lst):
a = i
b = i + num
if len(lst[a:b])<num:
break
rst.append(tuple(lst[a:b]))
return rst
Example:
>>> lst = np.array([10, 11, 12, 13, 26, 28])
>>> slice_list(lst,4)
[(10, 11, 12, 13), (11, 12, 13, 26), (12, 13, 26, 28)]
- 29.6k
- 16
- 45
- 203
My function needs two params(a,b) and if...break
, which seems redundant. Can it be made prettier?
def slice_list(lst,num):
"""
Given a list or Numpy array,
get consecutive tuples based on specific window.
"""
rst = []
for i,_ in enumerate(lst):
a = i
b = i + num
if len(lst[a:b])<num:
break
rst.append(tuple(lst[a:b]))
return rst
Given a list or Numpy array, get consecutive tuples based on specific window. """ rst = [] for i,_ in enumerate(lst): a = i b = i + num if len(lst[a:b])<num: break rst.append(tuple(lst[a:b])) return rst
Example:
>>> lst = np.array([10, 11, 12, 13, 26, 28])
>>> slice_list(lst,4)
[(10, 11, 12, 13), (11, 12, 13, 26), (12, 13, 26, 28)]
My function needs two params(a,b) and if...break
, which seems redundant. Can it be made prettier?
def slice_list(lst,num):
"""
Given a list or Numpy array, get consecutive tuples based on specific window. """ rst = [] for i,_ in enumerate(lst): a = i b = i + num if len(lst[a:b])<num: break rst.append(tuple(lst[a:b])) return rst
Example:
>>> lst = np.array([10, 11, 12, 13, 26, 28])
>>> slice_list(lst,4)
[(10, 11, 12, 13), (11, 12, 13, 26), (12, 13, 26, 28)]
My function needs two params(a,b) and if...break
, which seems redundant. Can it be made prettier?
def slice_list(lst,num):
"""
Given a list or Numpy array,
get consecutive tuples based on specific window.
"""
rst = []
for i,_ in enumerate(lst):
a = i
b = i + num
if len(lst[a:b])<num:
break
rst.append(tuple(lst[a:b]))
return rst
Example:
>>> lst = np.array([10, 11, 12, 13, 26, 28])
>>> slice_list(lst,4)
[(10, 11, 12, 13), (11, 12, 13, 26), (12, 13, 26, 28)]
My function needs two params(a,b) and if...break
, which seems redundant. Can it be made prettier?
def slice_list(lst,num):
rst = []
for i,_ in enumerate(lst):
a = i
b = i + num
if len(lst[a:b])<num:
break
rst.append(tuple(lst[a:b]))
return rst"""
Given a list or Numpy array, get consecutive tuples based on specific window. """ rst = [] for i,_ in enumerate(lst): a = i b = i + num if len(lst[a:b])<num: break rst.append(tuple(lst[a:b])) return rst
Example:
>>> lst = np.array([10, 11, 12, 13, 26, 28])
>>> slice_list(lst,4)
[(10, 11, 12, 13), (11, 12, 13, 26), (12, 13, 26, 28)]
My function needs two params(a,b) and if...break
, which seems redundant. Can it be made prettier?
def slice_list(lst,num):
rst = []
for i,_ in enumerate(lst):
a = i
b = i + num
if len(lst[a:b])<num:
break
rst.append(tuple(lst[a:b]))
return rst
Example:
>>> lst = np.array([10, 11, 12, 13, 26, 28])
>>> slice_list(lst,4)
[(10, 11, 12, 13), (11, 12, 13, 26), (12, 13, 26, 28)]
My function needs two params(a,b) and if...break
, which seems redundant. Can it be made prettier?
def slice_list(lst,num):
"""
Given a list or Numpy array, get consecutive tuples based on specific window. """ rst = [] for i,_ in enumerate(lst): a = i b = i + num if len(lst[a:b])<num: break rst.append(tuple(lst[a:b])) return rst
Example:
>>> lst = np.array([10, 11, 12, 13, 26, 28])
>>> slice_list(lst,4)
[(10, 11, 12, 13), (11, 12, 13, 26), (12, 13, 26, 28)]