Python 将列表拆分为多个片段并将其连接起来

Python 将列表拆分为多个片段并将其连接起来,python,list,slice,Python,List,Slice,我看到过与我类似的问题,但我所研究的都没有真正解决我的问题。 所以,基本上我想拆分一个列表,以便删除一些项并将其连接回来。这些项对应于元组列表给出的索引。 import numpy as np arr = ['x','y','z','a','b','c','d','e','f','g',2,3,4] indices = [(2,4),(7,9)] #INDEXES THAT NEED TO BE CUT OUT print ([list1[0:s] +list1[s+1:e] for s,e i

我看到过与我类似的问题,但我所研究的都没有真正解决我的问题。
所以,基本上我想拆分一个列表,以便删除一些项并将其连接回来。这些项对应于元组列表给出的索引。

import numpy as np
arr = ['x','y','z','a','b','c','d','e','f','g',2,3,4]
indices = [(2,4),(7,9)] #INDEXES THAT NEED TO BE CUT OUT
print ([list1[0:s] +list1[s+1:e] for s,e in indices])
#Returns: [['x', 'y', 'z', 'a'], ['x', 'y', 'z', 'a', 'b', 'c', 'd', 'e', 'f']]
我从中得到的这段代码几乎满足了我的需要,但我尝试将其调整为循环索引的第一个索引一次,但它执行了两次,并且不包括列表的其余部分。

我希望使用for循环或某种迭代器将最终列表从零索引拆分为第一个元组上的第一项,依此类推。
像这样的,

`final_arr = arr[0:indices[0][0]] + arr[indices[0][1]:indices[1][0]] + arr[indices[1][1]:]<br/>
#Returns: [['x','y','a','b','c','f','g',2,3,4]]`
`final_arr=arr[0:index[0][0]]+arr[index[0][1]:index[1][0]+arr[index[1][1]:]
#返回:['x','y','a','b','c','f','g',2,3,4]]`
如果有人可以使用for循环来完成,我会更容易了解您对问题的理解,然后我可以尝试适应使用较短的代码。

如下:

import numpy as np
arr = ['x','y','z','a','b','c','d','e','f','g',2,3,4]
indices = [(2,4),(7,9)] #INDEXES THAT NEED TO BE CUT OUT
print ([v for t in indices for i,v in enumerate(arr) if i not in range(t[0],t[1])])
输出:

['x', 'y', 'z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 2, 3, 4, 'x', 'y', 'z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 2, 3, 4]

使用
sorted
del
切片对索引进行排序。您需要
reverse=True
否则后面切片的索引不正确

for x, y in sorted(indices, reverse=True):
    del(arr[x:y])

print(arr)

>>> ['x', 'y', 'b', 'c', 'd', 'g', 2, 3, 4]
这与您使用时得到的结果相同

print(arr[0:indices[0][0]] + arr[indices[0][1]:indices[1][0]] + arr[indices[1][1]:])

>>> ['x', 'y', 'b', 'c', 'd', 'g', 2, 3, 4]

arr=['x','y','z','a','b','c','d','e','f','g',2,3,4]
索引=[(2,4)、(7,9)]#需要删除的索引
进口itertools
ignore=set(itertools.chain.from_iterable(map(lambda i:range(*i),index)))
out=[c代表idx,c代表枚举(arr),如果idx不在ignore中]
打印(输出)
打印(arr[0:索引[0][0]]+arr[索引[0][1]:索引[1][0]]+arr[索引[1][1]:])
产出

['x', 'y', 'b', 'c', 'd', 'g', 2, 3, 4]
['x', 'y', 'b', 'c', 'd', 'g', 2, 3, 4]

1-如果可以删除列表项:
我用这个例子来解释。我更改索引列表(已删除的项),因为总是会删除一个索引,从而更改列表的大小

arr = ['x','y','z','a','b','c','d','e','f','g',2,3,4]
indices = [2,5,5] #INDEXES THAT NEED TO BE CUT OUT

for index in indices:
    arr.pop(index)

final_arr = [arr]
print(final_arr)
输出:

[['x', 'y', 'a', 'b', 'c', 'f', 'g', 2, 3, 4]]
['x','y','a','b','c','f','g',2,3,4]

2-如果无法删除项目:
在这种情况下,有必要更改第二个索引!该数字与您想要的输出不匹配
指数=[(2,4)、(7,9)]的输出为:['x','y','a','b','c','d','f','g',2,3,4]

arr = ['x','y','z','a','b','c','d','e','f','g',2,3,4]
indices = [(2,4),(6,9)] #INDEXES THAT NEED TO BE CUT OUT

final_arr = arr[0:indices[0][0]] + arr[indices[0][1]-1:indices[1][0]] + arr[indices[1][1]-1:]
print(final_arr)
输出:

[['x', 'y', 'a', 'b', 'c', 'f', 'g', 2, 3, 4]]
['x','y','a','b','c','f','g',2,3,4]

你能加上最后的预期结果吗?这将有助于理解您的问题。当然,我刚刚添加了它。您的输出与您的问题不匹配。当我复制并粘贴代码时,预期结果中的值是错误的。我已根据您的
最终\u arr
逻辑添加了一个答案,但是输出与您在
#返回中描述的不同。如果不是预期的输出,请告诉我们。嗨@blakev,你能向我解释一下这个itertools.chain.from_iterable的真正功能吗?我认为这段代码可能有用。但我对Python还不熟悉,那些奇特的方法很难理解
itertools.chain.from_iterable
获取列表列表并将其压缩为“平面”列表。