List python函数按索引拆分列表

List python函数按索引拆分列表,list,function,python-2.7,split,indexing,List,Function,Python 2.7,Split,Indexing,我试图构建一个有效的函数,用于将任意大小的列表按任意给定数量的索引进行拆分。这种方法很有效,我花了几个小时才把它弄对(我讨厌在使用索引时容易出错) 我是不是想得太多了 代码: def lindexsplit(列表,*lindex): index=list(lindex) index.sort() templist1=[] templist2=[] templist3=[] 断开计数器=0 itemcounter=0 最终计数器=0 numberofbreaks=len(索引) totalitem

我试图构建一个有效的函数,用于将任意大小的列表按任意给定数量的索引进行拆分。这种方法很有效,我花了几个小时才把它弄对(我讨厌在使用索引时容易出错)

我是不是想得太多了

代码:

def lindexsplit(列表,*lindex):

index=list(lindex)
index.sort()
templist1=[]
templist2=[]
templist3=[]
断开计数器=0
itemcounter=0
最终计数器=0
numberofbreaks=len(索引)
totalitems=len(列表)
lastindexval=索引[(len(index)-1]
finalcounttrigger=(totalitems-(lastindexval+1))
对于列表中的项目:
itemcounter+=1
indexofitem=itemcounter-1
nextbreakindex=索引[breakcounter]
#比上次少

如果这个问题不属于主题,因为它属于“谢谢jonrsharpe!”:)
index = list(lindex)

index.sort()

templist1 = []
templist2 = []
templist3 = []

breakcounter = 0
itemcounter = 0
finalcounter = 0


numberofbreaks = len(index)
totalitems = len(List)

lastindexval = index[(len(index)-1)]
finalcounttrigger = (totalitems-(lastindexval+1))

for item in List:

    itemcounter += 1

    indexofitem = itemcounter - 1

    nextbreakindex = index[breakcounter]

    #Less than the last cut
    if breakcounter <= numberofbreaks:

        if indexofitem < nextbreakindex:

            templist1.append(item)

        elif breakcounter < (numberofbreaks - 1):

            templist1.append(item)

            templist2.append(templist1)

            templist1 = []

            breakcounter +=1

        else:

            if indexofitem <= lastindexval and indexofitem <= totalitems:

                templist1.append(item)

                templist2.append(templist1)

                templist1 = []

            else:

                if indexofitem >= lastindexval and indexofitem < totalitems + 1:

                    finalcounter += 1

                    templist3.append(item)

                    if finalcounter == finalcounttrigger:

                        templist2.append(templist3)


return templist2