我如何在我的列表末尾获取某些值,每隔一段时间获取这些值,并插入到 Python 中原始列表的某些位置?
How do I get certain values at the end of my list, get these values every so often, and insert in certain positions in my original list in Python?
假设我有一个这样的列表:
ls = [a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14]
我想通过以下方式获取此列表的最后六个条目:
new_ls = ls[-6:]
从这里开始,我想获取这个新列表并获取此 new_ls
中的每两个并将其插入我的 ls
中的每两个之间,以便我的输出如下所示:
output_ls = [a1,a2,a9,a10,a3,a4,a11,a12,a5,a6,a13,a14,a7,a8]
我该怎么做?谢谢!
这是我到目前为止在我的代码中尝试过的,total_ls 有 108 个元素:
new_ls1 = list(xy4b1.items())
new_ls2 = list(xy4b2.items())
total_ls = new_ls1 + new_ls2
size = 2
start = -36
test_ls = total_ls[start:start+size]
total_ls.insert(4,test_ls)
我只能将我的“最后一个元素”插入到我的原始列表中。我还没有完成 for
循环实现,因为我不知道如何实现。
Python 根据要求添加变量 NewList 的示例:
ls = ['a1','a2','a3','a4','a5','a6','a7','a8','a9','a10']
newList= ls[-4:]
ls = ls[0:len(ls)-4]
print(ls)
print(newList)
endList=[]
isPutFromNewList = True
amountToTakeFromNewList =2
amountOfOldListToHaveBetween=6
lsCounter=0;
newListCounter=0
i=0
while(len(endList)!=(len(ls)+len(newList))):
if((i%amountOfOldListToHaveBetween==0 and not isPutFromNewList) or (i%amountToTakeFromNewList==0 and isPutFromNewList)):
isPutFromNewList = not isPutFromNewList
if(isPutFromNewList and newListCounter<len(newList)):
endList.append(newList[newListCounter])
newListCounter=newListCounter+1
if(not isPutFromNewList and lsCounter<len(ls)):
endList.append(ls[lsCounter])
lsCounter=lsCounter+1
i= i+1
print("end list:",endList)
下面的 JS 示例,因为我写得很开心:
var ls = ['a1','a2','a3','a4','a5','a6','a7','a8','a9','a10'];
var newList= ls.slice(-4)
ls = ls.slice(0,ls.length-4)
console.log(ls);
console.log(newList);
var endList=[];
var isPutFromNewList = true;
var lsCounter=0;
var newListCounter=0;
for(var i=0 ;(endList.length!=(ls.length+newList.length));i++){
if(i%2==0){
isPutFromNewList = !isPutFromNewList;
}
if(isPutFromNewList && newListCounter<newList.length){
endList.push(newList[newListCounter]);
newListCounter++;
}
if(! isPutFromNewList && lsCounter<ls.length){
endList.push(ls[lsCounter]);
lsCounter++;
}
}
console.log("end list:",endList);
假设我有一个这样的列表:
ls = [a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14]
我想通过以下方式获取此列表的最后六个条目:
new_ls = ls[-6:]
从这里开始,我想获取这个新列表并获取此 new_ls
中的每两个并将其插入我的 ls
中的每两个之间,以便我的输出如下所示:
output_ls = [a1,a2,a9,a10,a3,a4,a11,a12,a5,a6,a13,a14,a7,a8]
我该怎么做?谢谢!
这是我到目前为止在我的代码中尝试过的,total_ls 有 108 个元素:
new_ls1 = list(xy4b1.items())
new_ls2 = list(xy4b2.items())
total_ls = new_ls1 + new_ls2
size = 2
start = -36
test_ls = total_ls[start:start+size]
total_ls.insert(4,test_ls)
我只能将我的“最后一个元素”插入到我的原始列表中。我还没有完成 for
循环实现,因为我不知道如何实现。
Python 根据要求添加变量 NewList 的示例:
ls = ['a1','a2','a3','a4','a5','a6','a7','a8','a9','a10']
newList= ls[-4:]
ls = ls[0:len(ls)-4]
print(ls)
print(newList)
endList=[]
isPutFromNewList = True
amountToTakeFromNewList =2
amountOfOldListToHaveBetween=6
lsCounter=0;
newListCounter=0
i=0
while(len(endList)!=(len(ls)+len(newList))):
if((i%amountOfOldListToHaveBetween==0 and not isPutFromNewList) or (i%amountToTakeFromNewList==0 and isPutFromNewList)):
isPutFromNewList = not isPutFromNewList
if(isPutFromNewList and newListCounter<len(newList)):
endList.append(newList[newListCounter])
newListCounter=newListCounter+1
if(not isPutFromNewList and lsCounter<len(ls)):
endList.append(ls[lsCounter])
lsCounter=lsCounter+1
i= i+1
print("end list:",endList)
下面的 JS 示例,因为我写得很开心:
var ls = ['a1','a2','a3','a4','a5','a6','a7','a8','a9','a10'];
var newList= ls.slice(-4)
ls = ls.slice(0,ls.length-4)
console.log(ls);
console.log(newList);
var endList=[];
var isPutFromNewList = true;
var lsCounter=0;
var newListCounter=0;
for(var i=0 ;(endList.length!=(ls.length+newList.length));i++){
if(i%2==0){
isPutFromNewList = !isPutFromNewList;
}
if(isPutFromNewList && newListCounter<newList.length){
endList.push(newList[newListCounter]);
newListCounter++;
}
if(! isPutFromNewList && lsCounter<ls.length){
endList.push(ls[lsCounter]);
lsCounter++;
}
}
console.log("end list:",endList);