Python Forum
How to extract two data types from a text file and store them as two separate lists
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to extract two data types from a text file and store them as two separate lists
#1
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!
Reply
#2
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Exclamation URGENT: How to plot data from text file. Trying to recreate plots from MATLAB JamieAl 4 3,544 Dec-03-2023, 06:56 AM
Last Post: Pedroski55
  function for extracting data from lists Paulman 9 2,804 Nov-09-2021, 04:00 PM
Last Post: Paulman
  Can we store value in file if we open file in read mode? prasanthbab1234 3 2,566 Sep-26-2020, 12:10 PM
Last Post: ibreeden
  Working with text data APK 4 2,505 Aug-22-2020, 04:48 AM
Last Post: buran
  Read data from a CSV file in S3 bucket and store it in a dictionary in python Rupini 3 6,981 May-15-2020, 04:57 PM
Last Post: snippsat
  Trying to extract Only the capitol letters from a string of text Jaethan 2 2,177 Feb-27-2020, 11:19 PM
Last Post: Marbelous
  Read text file, process data and print specific output Happythankyoumoreplease 3 2,917 Feb-20-2020, 12:19 PM
Last Post: jefsummers
  Store data in array kasper1903 4 3,105 Oct-04-2019, 12:43 PM
Last Post: kasper1903
  Convert text from an image to a text file Evil_Patrick 5 4,281 Jul-30-2019, 07:57 PM
Last Post: DeaD_EyE
  reading text file and writing to an output file precedded by line numbers kannan 7 10,384 Dec-11-2018, 02:19 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020