Reputation: 27
I want to choose values with label '1' at the end and randomly select 1000 comments among the selected values.
This is the text version of the code that creates the output above:
with open('movie_ratings.txt', 'r') as f:
lines = f.read().splitlines()
lines = lines[1:]
sentences = [line.split('\t') for line in lines]
sentences
Upvotes: 0
Views: 506
Reputation: 37832
To select only sentences where the last element is '1':
sentences = [line.split('\t') for line in lines if line.split('\t')[-1] == '1']
For the random selection, this can help you on your way: Python random.choice() function to select random item from a List and Set
Upvotes: 0
Reputation: 2301
First load in the lines
lines = open('movie_ratings.txt').read().splitlines()[1:]
sentences = [line.split('\t') for line in lines]
Now we keep comments where that last value is '1'
comments_to_keep = [
comment for rating_id, comment, flag in sentences
if flag == '1'
]
Now we take a sample of those comments
import random
sample = random.sample(comments_to_keep, 1000)
Upvotes: 1