拆分 python 中的列表,以元素作为分隔符?
Split a list in python with an element as the delimiter?
我想从一个有很多重复元素的列表中创建子列表,即。
l = ['a', 'b', 'c', 'c', 'b', 'a', 'b', 'c', 'b', 'a']
无论 'a'
在哪里开始,列表都应该被拆分。 (最好删除 'a'
但不是必须的)
因此:
l = [ ['b', 'c', 'c', 'b'], ['b', 'c', 'b'] ]
我已经尝试 new_list = [x.split('a')[-1] for x in l]
但我没有得到想要的 "New list" 效果。
写的时候,
new_list = [x.split('a')[-1] for x in l]
你本质上是在表演,
result = []
for elem in l:
result.append(elem.split('a')[-1])
也就是说,您将 l
中包含的每个字符串拆分为字母 'a'
,并将每个字符串的最后一个元素收集到结果中。
这是您正在寻找的机制的一种可能实现方式:
def extract_parts(my_list, delim):
# Locate the indices of all instances of ``delim`` in ``my_list``
indices = [i for i, x in enumerate(my_list) if x == delim]
# Collect each end-exclusive sublist bounded by each pair indices
sublists = []
for i in range(len(indices)-1):
part = my_list[indices[i]+1:indices[i+1]]
sublists.append(part)
return sublists
使用这个函数,我们有
>>> l = ['a', 'b', 'c', 'c', 'b', 'a', 'b', 'c', 'b', 'a']
>>> extract_parts(l, 'a')
[['b', 'c', 'c', 'b'], ['b', 'c', 'b']]
您可以使用 zip 和 enumerate 来做到这一点。创建一个用于分离的 id 列表,然后在这些点上将其拆分。
size = len(l)
id_list = [id + 1 for id, val in
enumerate(test_list) if val == 'a']
result = [l[i:j] for i, j in zip([0] + id_list, id_list +
([size] if id_list[-1] != size else []))]
为每个要拆分的元素创建一个计数器数组,然后以这种方式编写条件:
l = ['a', 'b', 'c', 'c', 'b', 'a', 'b', 'c', 'b', 'a']
counters = [0,0,0] #to count instances
index = 0 #index of l
startIndex = 0 #where to start split
endIndex = 0 #where to end split
splitLists = [] #container for splits
for element in l:
if element == 'a': #find 'a'
counters[0] += 1 #increase counter
if counters[0] == 1: #if first instance
startIndex = index + 1 #start split after
if counters[0] == 2:
endIndex = index #if second instance
splitList = l[startIndex:endIndex] #end split here
counters[0] = 1 #make second starting location
startIndex = index + 1
splitLists.append(splitList) #append to main list of lists
index += 1
print(splitLists)
所以基本上您是在列表中找到匹配模式的开始和结束索引。您使用这些来拆分列表,并将此列表附加到列表的主列表(2d 列表)中。
不包括分隔符
import itertools
lst = ['a', 'b', 'c', 'c', 'b', 'a', 'b', 'c', 'b', 'a']
delimiter = lst[0]
li=[list(value) for key,value in itertools.groupby(lst, lambda e: e == delimiter) if not key]
print(li)
说明:groupby函数会在每次键改变时创建一个新组
Key value
True itertools._grouper object pointing to group 'a'
False itertools._grouper object pointing to group 'b', 'c', 'c', 'b'
True itertools._grouper object pointing to group 'a'
False itertools._grouper object pointing to group 'b', 'c', 'b'
True itertools._grouper object pointing to group 'a'
在if条件检查key是否为false时,return itertools._grouper对象然后传递itertool对象 列出。
我想从一个有很多重复元素的列表中创建子列表,即。
l = ['a', 'b', 'c', 'c', 'b', 'a', 'b', 'c', 'b', 'a']
无论 'a'
在哪里开始,列表都应该被拆分。 (最好删除 'a'
但不是必须的)
因此:
l = [ ['b', 'c', 'c', 'b'], ['b', 'c', 'b'] ]
我已经尝试 new_list = [x.split('a')[-1] for x in l]
但我没有得到想要的 "New list" 效果。
写的时候,
new_list = [x.split('a')[-1] for x in l]
你本质上是在表演,
result = []
for elem in l:
result.append(elem.split('a')[-1])
也就是说,您将 l
中包含的每个字符串拆分为字母 'a'
,并将每个字符串的最后一个元素收集到结果中。
这是您正在寻找的机制的一种可能实现方式:
def extract_parts(my_list, delim):
# Locate the indices of all instances of ``delim`` in ``my_list``
indices = [i for i, x in enumerate(my_list) if x == delim]
# Collect each end-exclusive sublist bounded by each pair indices
sublists = []
for i in range(len(indices)-1):
part = my_list[indices[i]+1:indices[i+1]]
sublists.append(part)
return sublists
使用这个函数,我们有
>>> l = ['a', 'b', 'c', 'c', 'b', 'a', 'b', 'c', 'b', 'a']
>>> extract_parts(l, 'a')
[['b', 'c', 'c', 'b'], ['b', 'c', 'b']]
您可以使用 zip 和 enumerate 来做到这一点。创建一个用于分离的 id 列表,然后在这些点上将其拆分。
size = len(l)
id_list = [id + 1 for id, val in
enumerate(test_list) if val == 'a']
result = [l[i:j] for i, j in zip([0] + id_list, id_list +
([size] if id_list[-1] != size else []))]
为每个要拆分的元素创建一个计数器数组,然后以这种方式编写条件:
l = ['a', 'b', 'c', 'c', 'b', 'a', 'b', 'c', 'b', 'a']
counters = [0,0,0] #to count instances
index = 0 #index of l
startIndex = 0 #where to start split
endIndex = 0 #where to end split
splitLists = [] #container for splits
for element in l:
if element == 'a': #find 'a'
counters[0] += 1 #increase counter
if counters[0] == 1: #if first instance
startIndex = index + 1 #start split after
if counters[0] == 2:
endIndex = index #if second instance
splitList = l[startIndex:endIndex] #end split here
counters[0] = 1 #make second starting location
startIndex = index + 1
splitLists.append(splitList) #append to main list of lists
index += 1
print(splitLists)
所以基本上您是在列表中找到匹配模式的开始和结束索引。您使用这些来拆分列表,并将此列表附加到列表的主列表(2d 列表)中。
不包括分隔符
import itertools
lst = ['a', 'b', 'c', 'c', 'b', 'a', 'b', 'c', 'b', 'a']
delimiter = lst[0]
li=[list(value) for key,value in itertools.groupby(lst, lambda e: e == delimiter) if not key]
print(li)
说明:groupby函数会在每次键改变时创建一个新组
Key value
True itertools._grouper object pointing to group 'a'
False itertools._grouper object pointing to group 'b', 'c', 'c', 'b'
True itertools._grouper object pointing to group 'a'
False itertools._grouper object pointing to group 'b', 'c', 'b'
True itertools._grouper object pointing to group 'a'
在if条件检查key是否为false时,return itertools._grouper对象然后传递itertool对象 列出。