Python Forum
How to extract two data types from a text file and store them as two separate lists - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: How to extract two data types from a text file and store them as two separate lists (/thread-1617.html)



How to extract two data types from a text file and store them as two separate lists - banf - Jan-16-2017

I'm currently making a program which will compress a user sentence into two variables unique_words and positions. Unique words will be all the unique words from the users sentence, so for example "the cat sat on the mat" the unique words will be "the, cat, sat, on, mat" and the positions will be 123415. I'm saving both of these in a text file. 

Then what I want to be able to do is extract these back from the text file, and store them in two separate variables, so for example I want my code to look something like so.

    list_1 = [the, cat, sat, on, mat]
    list_2 = [1, 2, 3, 4, 1, 5]
    list_3 = []
    
    for i in list_2:
      list_3.append(list_1[i])
This is what I want to do in the end, but I am clueless as to how to extract the positions and unique_words, and store them in two separate variables!


RE: How to extract two data types from a text file and store them as two separate lists - Larz60+ - Jan-16-2017

list_1 are strings, therefore each must be surrounded by quotes

list_2 - Remember lists are zero based (1st element is index 0), so
the for loop will fail with an index out of range error

You should add a print statement for list_3 at end to verify it's what you want