Python 使用分隔符拆分列表
我编写了一个函数,它获取两个参数:一个列表和一个值,该值出现在前面给出的列表中(sep)。该函数的目的是拆分给定的列表,并返回列表中的多个列表,而不返回Write函数第二个参数中指定的值。因此,使用def split_list([1,2,3,2,1],2)-->结果将是[[1]、[3]、[1]]。拆分功能很好,但结果会将函数的第二个值(sep)保留在单独的列表中。我想不出解决这个问题的办法。提前谢谢Python 使用分隔符拆分列表,python,Python,我编写了一个函数,它获取两个参数:一个列表和一个值,该值出现在前面给出的列表中(sep)。该函数的目的是拆分给定的列表,并返回列表中的多个列表,而不返回Write函数第二个参数中指定的值。因此,使用def split_list([1,2,3,2,1],2)-->结果将是[[1]、[3]、[1]]。拆分功能很好,但结果会将函数的第二个值(sep)保留在单独的列表中。我想不出解决这个问题的办法。提前谢谢 def split_list(l, sep): occurence = [i for i, x
def split_list(l, sep):
occurence = [i for i, x in enumerate(l) if x == sep]
newlist=[]
newlist.append(l[:occurence[0]])
for i in range(0,len(occurence)):
j=i+1
if j < len(occurence):
newlist.append(l[occurence[i]:occurence[j]])
i+=1
newlist.append(l[occurence[-1]:])
return newlist
def拆分列表(左、九月):
发生率=[i代表i,x在枚举(l)中,如果x==sep]
新列表=[]
newlist.append(l[:出现[0]])
对于范围(0,len(发生))中的i:
j=i+1
如果j
这个怎么样:
def split_list(l, sep):
nl = [[]]
for el in l:
if el == sep:
nl.append([])
else:
# Append to last list
nl[-1].append(el)
return nl
或者使用您的方法,通过使用事件列表:
def split_list(l, sep):
# occurences
o = [i for i, x in enumerate(l) if x == sep]
nl = []
# first slice
nl.append(l[:o[0]])
# middle slices
for i in range(1, len(o)):
nl.append(l[o[i-1]+1:o[i]])
# last slice
nl.append(l[o[-1]+1:])
return nl
这个怎么样:
def split_list(l, sep):
nl = [[]]
for el in l:
if el == sep:
nl.append([])
else:
# Append to last list
nl[-1].append(el)
return nl
或者使用您的方法,通过使用事件列表:
def split_list(l, sep):
# occurences
o = [i for i, x in enumerate(l) if x == sep]
nl = []
# first slice
nl.append(l[:o[0]])
# middle slices
for i in range(1, len(o)):
nl.append(l[o[i-1]+1:o[i]])
# last slice
nl.append(l[o[-1]+1:])
return nl
如果x!=sep,则在枚举(l)中使用[list(x)表示i,x]如果x!=sep,则在枚举(l)中使用[list(x)表示i,x]您可以使用以下列表理解和功能拆分列表:
>>> l=[1,2,3,2,1,8,9]
>>> oc= [i for i, x in enumerate(l) if x == 2]
>>> [l[i:j] if 2 not in l[i:j] else l[i+1:j] for i, j in zip([0]+oc, oc+[None])]
[[1], [3], [1, 8, 9]]
def split_list(l, sep):
occurence = [i for i, x in enumerate(l) if x == sep]
return [l[i:j] if sep not in l[i:j] else l[i+1:j] for i, j in zip([0]+occurence, occurence+[None])]
因此,对于您的功能:
>>> l=[1,2,3,2,1,8,9]
>>> oc= [i for i, x in enumerate(l) if x == 2]
>>> [l[i:j] if 2 not in l[i:j] else l[i+1:j] for i, j in zip([0]+oc, oc+[None])]
[[1], [3], [1, 8, 9]]
def split_list(l, sep):
occurence = [i for i, x in enumerate(l) if x == sep]
return [l[i:j] if sep not in l[i:j] else l[i+1:j] for i, j in zip([0]+occurence, occurence+[None])]
您可以使用以下列表理解和功能拆分列表:
>>> l=[1,2,3,2,1,8,9]
>>> oc= [i for i, x in enumerate(l) if x == 2]
>>> [l[i:j] if 2 not in l[i:j] else l[i+1:j] for i, j in zip([0]+oc, oc+[None])]
[[1], [3], [1, 8, 9]]
def split_list(l, sep):
occurence = [i for i, x in enumerate(l) if x == sep]
return [l[i:j] if sep not in l[i:j] else l[i+1:j] for i, j in zip([0]+occurence, occurence+[None])]
因此,对于您的功能:
>>> l=[1,2,3,2,1,8,9]
>>> oc= [i for i, x in enumerate(l) if x == 2]
>>> [l[i:j] if 2 not in l[i:j] else l[i+1:j] for i, j in zip([0]+oc, oc+[None])]
[[1], [3], [1, 8, 9]]
def split_list(l, sep):
occurence = [i for i, x in enumerate(l) if x == sep]
return [l[i:j] if sep not in l[i:j] else l[i+1:j] for i, j in zip([0]+occurence, occurence+[None])]