Python 是否使用分隔符将新列表插入现有列表?
我有一个存储坐标元组的列表。例如:Python 是否使用分隔符将新列表插入现有列表?,python,list,tuples,Python,List,Tuples,我有一个存储坐标元组的列表。例如: coords = [(0,0), (1,1), (4,4), (4,6), (9,2), (34,14), (43,6)] start = (1,1) end = (9,2) to_add = [(50,50), (71,31), (84,24)] 我还有另外两个元组用作分隔符(它们总是在coords列表中),例如: coords = [(0,0), (1,1), (4,4), (4,6), (9,2), (34,14), (43,6)] start
coords = [(0,0), (1,1), (4,4), (4,6), (9,2), (34,14), (43,6)]
start = (1,1)
end = (9,2)
to_add = [(50,50), (71,31), (84,24)]
我还有另外两个元组用作分隔符(它们总是在coords
列表中),例如:
coords = [(0,0), (1,1), (4,4), (4,6), (9,2), (34,14), (43,6)]
start = (1,1)
end = (9,2)
to_add = [(50,50), (71,31), (84,24)]
开始
点并不总是列在结束
点之前,但它们总是在协调
列表中
最后,我有另一个单独的坐标列表,例如:
coords = [(0,0), (1,1), (4,4), (4,6), (9,2), (34,14), (43,6)]
start = (1,1)
end = (9,2)
to_add = [(50,50), (71,31), (84,24)]
基本上,我想从coords
列表中删除所有项目
位于开始
和结束
元组之间,并将添加到\u添加
项中。最终结果如下:
coords = [(0,0), (1,1), (50,50), (71,31), (84,24), (9,2), (34,14), (43,6)]
我如何才能做到这一点?使用
sorted()
和list.index()函数的解决方案:
coords = [(0,0), (1,1), (4,4), (4,6), (9,2), (34,14), (43,6)]
end = (1,1)
start = (9,2)
to_add = [(50,50), (71,31), (84,24)]
bounds = sorted((end, start)) # getting arranged boundaries
coords[coords.index(bounds[0])+1:coords.index(bounds[1])] = to_add
print(coords)
输出:
[(0, 0), (1, 1), (50, 50), (71, 31), (84, 24), (9, 2), (34, 14), (43, 6)]
使用sorted()
和list.index()函数的解决方案:
coords = [(0,0), (1,1), (4,4), (4,6), (9,2), (34,14), (43,6)]
end = (1,1)
start = (9,2)
to_add = [(50,50), (71,31), (84,24)]
bounds = sorted((end, start)) # getting arranged boundaries
coords[coords.index(bounds[0])+1:coords.index(bounds[1])] = to_add
print(coords)
输出:
[(0, 0), (1, 1), (50, 50), (71, 31), (84, 24), (9, 2), (34, 14), (43, 6)]
将列表分为三个不同的列表,即从0索引到开始坐标索引,从开始坐标索引到结束坐标的另一个列表,以及从结束坐标索引到列表最后一个元素的最后一个列表。现在只添加第一个和最后一个列表,并添加到列表中
所需输出的代码段
start = (1,1)
end = (9,2)
start, end = sorted((end, start))
to_add = [(50,50), (71,31), (84,24)]
coords = coords[:coords.index(start)+1] + to_add + coords[coords.index(end):]
输出
[(0, 0), (1, 1), (50, 50), (71, 31), (84, 24), (9, 2), (34, 14), (43, 6)]
将列表分为三个不同的列表,即从0索引到开始坐标索引,从开始坐标索引到结束坐标的另一个列表,以及从结束坐标索引到列表最后一个元素的最后一个列表。现在只添加第一个和最后一个列表,并添加到列表中
所需输出的代码段
start = (1,1)
end = (9,2)
start, end = sorted((end, start))
to_add = [(50,50), (71,31), (84,24)]
coords = coords[:coords.index(start)+1] + to_add + coords[coords.index(end):]
输出
[(0, 0), (1, 1), (50, 50), (71, 31), (84, 24), (9, 2), (34, 14), (43, 6)]
在提问之前你试过什么吗?开始练习下面给出的所有例子。最终你会开始想出解决问题的办法。尝试一下这些想法——如果你被卡住了,回来问个问题。欢迎来到StackOverflow。请阅读并遵循帮助文档中的发布指南。在这里申请。StackOverflow不是设计、编码、研究或教程服务。请使用索引
查找列表中的两个元组。使用字符串切片从要保留的片段构建新列表。这足以让你行动起来吗?谢谢@Prune,我会看一看。问之前你试过什么吗?开始练习所有给出的例子。最终你会开始想出解决问题的办法。尝试一下这些想法——如果你被卡住了,回来问个问题。欢迎来到StackOverflow。请阅读并遵循帮助文档中的发布指南。在这里申请。StackOverflow不是设计、编码、研究或教程服务。请使用索引
查找列表中的两个元组。使用字符串切片从要保留的片段构建新列表。这足以让你动起来吗?谢谢@Prune,我会看一看。谢谢,即使它更清楚地理解,当开始元组和结束元组交换时,它似乎不起作用。对不起,我没有注意到这一点。我们可以对坐标进行排序,并重新分配起始坐标和结束坐标。我已经在我的回答中做了更改谢谢你的编辑,你的代码帮助我更好地理解,但我接受了另一个类似的回答,因为它之前已经发布了+1请允许我帮忙!!谢谢,即使它更清楚地理解,当开始元组和结束元组交换时,它似乎不起作用。对不起,我没有注意到这一点。我们可以对坐标进行排序,并重新分配起始坐标和结束坐标。我已经在我的回答中做了更改谢谢你的编辑,你的代码帮助我更好地理解,但我接受了另一个类似的回答,因为它之前已经发布了+1请允许我帮忙!!