Reading random lines from a file in Python that don't repeat untill 4 other lines have passed

Reading random lines from a file in Python that don't repeat untill 4 other lines have passed

python read random line from large file
how to print a random line from a text file in python
how to choose a random line from a text file
start reading file from specific line python
how to read a specific line from a text file in python
python read file line by line
write a python program to assess if a file is closed or not.
write a python program to remove newline characters from a file

So I am trying to make a program that can help people with learning new languages but I am already stuck at the beginning. One of the requirements is to let Python print the lines in a random order. So I made this.

import random

def randomline(file):
    with open(file) as f:

But now I got a problem with one of the other requirements. There has to be 4 other words in between before the word can show again and I have no idea how to do that.

I have a very primitive solution for you:

import random 

def randomline(file):
    with open(file) as f:
        return random.choice(lines)

isOccuredInLastFourExistence = True
LastFourWords = []

file = "text_file.txt"
for i in range(0,15):
    new_word = randomline(file)
    if new_word in LastFourWords:
        print("I have skipped")
    if(len(LastFourWords)) > 4:

The file looked like this:

The output looks like:(Showing only partial result)



['New\n', 'Example\n']

['New\n', 'Example\n', 'After\n']

['New\n', 'Example\n', 'After\n', 'Some\n']
I have skipped

['New\n', 'Example\n', 'After\n', 'Some\n']

['Example\n', 'After\n', 'Some\n', 'Please\n']
I have skipped

['Example\n', 'After\n', 'Some\n', 'Please\n']

['After\n', 'Some\n', 'Please\n', 'Only\n']
['Some\n', 'Please\n', 'Only\n', 'Word']

So every time you have something which is already present in your list, it will be skipped. And the list clears the first position element when there are more than 4 elements.

How do I read a random line from one file?, You want to use random.choice import random with open(filename) as f: lines = f. readlines() print(random.choice(lines)). Python has a module that is the random module by using the random module to get a random line from the text file(.txt). The following are constraints to get a random line: The file path must be specified to open the file on the computer. We must specify the mode of the file while the file is opening. Importing Random Module:

you can use a queue:

# create list with empty elements against which choice is checked
queue = 4*['']

def randomline(file):
    with open(file) as f:
        choice = random.choice(lines)
        if not choice in queue:

            # appendcurrent word to the queue
            # remove the first element of the list

Print random line from txt file?, Python can open, read and write into memory VERY big number of lines in just a moment. So if you don't have like a 1 GB file of words, just load it all and then� Open a file; Close a file. Python provides inbuilt functions for creating, writing and reading files. There are two types of files that can be handled in python, normal text files and binary files (written in binary language, 0s and 1s). In this article, we are going to study about reading line by line from a file. Reading line by line Using

You can utilise deque from the collections library. This will allow you to specify a max length for your seen words list. As you append items to the list, if your list is at the max length and you append a new item the oldest item will be remove. This allows you to make a cache. So if you create a list using deque with max length 4. Then you chose a word and check if its in the list, If it is then chose another word, if its not in the list then print the word and add it to the list. you dont have to worry about managing the items in the list as they oldest will automatically drop out when you append something new

from collections import deque
from random import choice, sample

with open('test.dat') as words_file:
    words = words_file.readlines()
    word_cache = deque(maxlen=4)
    for _ in range(30):
        word = choice(words).strip()
        while word in word_cache:
            word = choice(words).strip()

reading a random line from a text file : learnpython, We do need to read the whole file, but we don't have to read it all at once: import random def randomLine(file_object): "Retrieve a random line from a file,� In the comments, it has been asked if this solution reads the whole file first, and then iterates over the file a second time. I believe that it does not. The with open('a') as f line opens a file handle, but does not read the file. f is an iterator, so its contents are not read until requested.

I would use linecache. It's from the standard library and allows you to select a specific line. If you know the number of lines in your file, this could work:

import linecache
import random

def random_lines(filename, repeat_after=4):

    n_lines = len(open(filename, "r").readlines())
    last_indices = []

    while True:

        index = random.randint(1, n_lines)

        if index not in last_indices:

            last_indices = last_indices[-repeat_after:]

            line = linecache.getline(filename, index)
            yield line

This will create a generate which will output a random line from your file without needing to keep your lines in memory (which is great if you start having many lines).

As for your requirement of only allowing repetition after n number of times. This will take care of it. However, this has a very small chance of getting stuck in an infinite loop.

Another approach would be to create a list with all indices (i.e. line number), shuffle it, and then loop through them. This has the advantage of not risking to be in an infinite loop, but this also means that you'll need to go through every other line before you see the same line again, which may not be ideal for you.

Retrieving a Line at Random from a File of Unknown Size, The issue is, when Python reads a file, it reads that file and once it gets to the end , it won't go back and delete the lines. My current thinking was that I could write� import pandas import random n = 1000000 #number of records in file s = 10000 #desired sample size filename = "data.txt" skip = sorted(random.sample(xrange(n),n-s)) df = pandas.read_csv(filename, skiprows=skip) would be better if read_csv had a keeprows, or if skiprows took a callback func instead of a list. With header and unknown file length:

Python: Choose random line from file, then delete that line , If you truly mean a random line from a file (as in you're implementing something like the stream of text (in other words for the case where the data won't necessarily fit in memory). I'll like How do I read mutiple .txt files from folder in python? A Python program can read a text file using the built-in open() function. For example, the Python 3 program below opens lorem.txt for reading in text mode, reads the contents into a string variable named contents, closes the file, and prints the data.

How to select a random line from a file in Python, You don't say how big the file is. If it is relatively small - read the whole file in ( using readlines(), and then use random.choice() [code]import random def� One of the most common tasks that you can do with Python is reading and writing files. Whether it’s writing to a simple text file, reading a complicated server log, or even analyzing raw byte data, all of these situations require reading or writing a file. In this tutorial, you’ll learn: What makes up a file and why that’s important in Python

How to randomly choose a line from a TXT file in Python 3, file in Python. By using the random module we can get random line from the text file (.txt). “r” mode: In this mode, you must able t0 read the file. “w” mode: In � Python Exercises, Practice and Solution: Write a Python program to read a random line from a file.