循环迭代不删除每个列表项

时间:2019-05-11 18:57:04

标签: python

我正在用python实现游程长度编码,并且遇到了一个问题,即它不会删除删除列表中的每个项目,还有很多其他错误,我不知道从哪里开始。

在每个循环中增加一个变量,然后弹出该变量,而不是迭代的var的索引。

您需要上下文的其余代码:     #用户输入     print(“输入您要压缩的字符串”)     inputstring = input(“>”)     print(“”)

#convert to an array
inputarray = list(inputstring)

#define variables and lists
output = []
loops = 0
charloops = 0
amount = 0

# ommitted ", ', and # to avoid it interacting with code. I might be able to make it read from a comment in the text file and assign to variable in the final version.
characterString = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!$%&\()*+,-./:;?@[\\]^_`{|}~ \t\n\r\x0b\x0c"

#So you don't need to type out all off the above in list formatting
characters = list(characterString)
print(characters)

for character in inputarray:


    internalloops = 0
    while True:
        #this loops around the character to see if it is a certain character
        try:
            charloops = charloops + 1
            char = characters[charloops]
        except IndexError:
            charloops = 0
            internalloops = internalloops + 1
            if internalloops > 2:
                break

        if character == char:
            print("Character correct")
            #if it is the looped character, look in front. 
            deletelist = []
            while True:
               #the purpose of this while true is to prevent it running the next character until it has finished looking in front

                    #I use this because otherwise I would have to do an index error over the entire if/else below and that
                    #would hide errors from the deletion script, which currently doesn't work
                    try:
                        loopsAmount = inputarray[loops+amount]

                    except IndexError:
                        print("At end of list")
                        break

                    if loopsAmount == char:
                        amount += 1
                        local = int((str(loops) + str(amount)))
                        deletelist.append(local)



                        print("Found a character number: " + str(amount) + " | char = " + char)

问题出在哪里

                    else:
                        print("Deleting")
                        print(deletelist)
                        #Now you delete the characters

                        for a in deletelist:
                            inputarray.pop((deletelist.index(a)))
                            print(inputarray)
                            deletelist.pop((deletelist.index(a)))
                            print(deletelist)


                        deletelist.pop(1)
                        print(deletelist)

                        print("Adding")
                        #add the compressed data
                        appendme = (str(char) + str(amount))
                        output.append(appendme)
                        print(appendme)
                        print(output)
                        print("")

                        #reset amount ready for the next one
                        amount = 0

                        #now break the loop. It will look through the rest of the characters in the next loop
                        #-futile-but it doesn't pose much of a performance impact. I may optimise later although this is
                        #only a exercise to get better with data.
                        break

print("Output: ")
print(output)

while True:
    a = 1
    b = a*2
    a = b/1.5

0 个答案:

没有答案