简体   繁体   中英

Don't get two items when using .split() function

Okay, I really hope someone can help me with this one as I can't figure it out. I have a list:

list = ["a1 = True", "a2 = False", "a3 = True"]

and then I run my code to split them:

1: for answer in list:
2:     temp_list = answer.split("=")
3:     new_answer = temp_list[0].strip()
4:     list[list.index(answer)] = new_answer

This part of the code actually WORKS and gives me a list with a1, a2 and a3 all seperated The part that doesn't work is when I simply change the index in line 3 to 1:

1: for answer in list:
2:     temp_list = answer.split("=")
3:     new_answer = temp_list[1].strip()
4:     list[list.index(answer)] = new_answer

in this case it tells me that in line 3 I'm out of the lists range and I cannot see how that could possibly be... could anyone please help as I'm slowly driving into madness because of that...?

PS don't ask why I'm not using a library instead of an array, it's because of the way I get the information into the programm from a text file which is line by line like that

For those Who wanted to see all of my code, here it is. book is a string variable, chapter an integer. Those two are meant to change between the books that's why they are variables. As you can see, I filter out blank lines and use "---" as a seperator between them.

def answers():
    answers_file = open(book + str(chapter) + "/answers.txt")
    answers_string = "".join(answers_file.readlines())
    answers_list = answers_string.split("---")

    for answer in answers_list:
        new_answer = answer.strip("\n")
        temp_list = new_answer.split("=")
        new_answer = temp_list[0].strip()
        answers_list[answers_list.index(answer)] = new_answer

    return answers_list


def answer_key():
    answers_file = open(book + str(chapter) + "/answers.txt")
    answers_string = "".join(answers_file.readlines())
    answers_list = answers_string.split("---")
    answer_key_list = []

    for answer in answers_list:
        new_answer = answer.strip("\n")
        temp_list = new_answer.split("=")
        key = temp_list[1].strip()
        answer_key_list.append(key)

    return answer_key_list

PPS I found the issue and it had nothing to do with my code. It was an issue in the sample text file I made which effed up the code... just wanted you to know from the get go that I'm an idiot;)

You could append your answers in a new list. That way you would have 2 different sets which are not conflicting with each other.

list = ["a1 = True", "a2 = False", "a3 = True"]
new_list = []
new_list2 = []

for answer in list:
    temp_list = answer.split("=")
    new_answer = temp_list[0].strip()
    #list[list.index(answer)] = new_answer
    new_list.append(new_answer)
    
print(new_list)


for answer in list:
    temp_list = answer.split("=")
    new_answer = temp_list[1].strip()
    #list[list.index(answer)] = new_answer
    new_list2.append(new_answer)
    
print(new_list2)

Or you can use this below script as per my comments

list = ["a1 = True", "a2 = False", "a3 = True"]

print(list)

for answer in list:
    temp_list = answer.split("=")
    new_answer = temp_list[0].strip()
    list[list.index(answer)] = new_answer
    #new_list.append(new_answer)
    
print(list)
list = ["a1 = True", "a2 = False", "a3 = True"]

for answer in list:
    temp_list = answer.split("=")
    new_answer = temp_list[1].strip()
    list[list.index(answer)] = new_answer
    #new_list2.append(new_answer)
    
print(list)

But I see that you have updated your second function and looks like you are good.

It was a good exercise for me.

Cheers. H

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM